From 9a6c5ba62f4aaaee2ff25f61cc35be4801ce11cd Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Sun, 22 Jan 2023 16:13:24 +0200 Subject: [PATCH] Additional lint checks before publishing (#5459) --- .eslintrc.js | 3 +- bin/cli.js | 11 +-- lib/dialects/postgres/schema/pg-compiler.js | 4 +- lib/knex-builder/Knex.js | 2 +- lib/migrations/migrate/MigrationGenerator.js | 4 +- lib/schema/builder.js | 5 +- lib/schema/columnbuilder.js | 5 +- lib/schema/tablebuilder.js | 5 +- lib/schema/viewbuilder.js | 1 - package.json | 12 ++-- scripts/clean.js | 34 +++++----- scripts/update_gitignore_for_tsc_output.js | 68 ++++++++++--------- test/integration/dialects/oracledb.js | 6 +- test/integration2/dialects/oracledb.spec.js | 9 ++- .../helpers/migration-test-helper.js | 6 +- 15 files changed, 94 insertions(+), 81 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 151ec4be..afb67d97 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -10,11 +10,12 @@ module.exports = { 'plugin:import/warnings', 'prettier', ], - plugins: ['import', 'mocha-no-only'], + plugins: ['prettier', 'import', 'mocha-no-only'], rules: { 'mocha-no-only/mocha-no-only': ['error'], 'no-unused-vars': [warning, { vars: 'all', args: 'none' }], 'no-console': 'off', + 'no-empty': 'off', 'no-var': 2, 'no-debugger': warning, 'prefer-const': warning, diff --git a/bin/cli.js b/bin/cli.js index 3232ceb2..04d73ce6 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -218,7 +218,7 @@ function invoke() { .action(async (name) => { try { const opts = commander.opts(); - const instance = await initKnex(env, opts, true); // Skip config check, we don't really care about client when creating migrations + const instance = await initKnex(env, opts, true); // Skip config check, we don't really care about client when creating migrations const ext = getMigrationExtension(env, opts); const configOverrides = { extension: ext }; @@ -233,7 +233,7 @@ function invoke() { success(color.green(`Created Migration: ${name}`)); }) .catch(exit); - } catch(err) { + } catch (err) { exit(err); } }); @@ -397,7 +397,8 @@ function invoke() { } if (opts.timestampFilenamePrefix) { - configOverrides.timestampFilenamePrefix = opts.timestampFilenamePrefix; + configOverrides.timestampFilenamePrefix = + opts.timestampFilenamePrefix; } instance.seed @@ -406,9 +407,9 @@ function invoke() { success(color.green(`Created seed file: ${name}`)); }) .catch(exit); - } catch(err) { + } catch (err) { exit(err); - } + } }); commander diff --git a/lib/dialects/postgres/schema/pg-compiler.js b/lib/dialects/postgres/schema/pg-compiler.js index d164ce84..baa41156 100644 --- a/lib/dialects/postgres/schema/pg-compiler.js +++ b/lib/dialects/postgres/schema/pg-compiler.js @@ -120,7 +120,9 @@ class SchemaCompiler_PG extends SchemaCompiler { refreshMaterializedView(viewName, concurrently = false) { this.pushQuery({ - sql: `refresh materialized view${concurrently ? " concurrently" : ""} ${this.formatter.wrap(viewName)}`, + sql: `refresh materialized view${ + concurrently ? ' concurrently' : '' + } ${this.formatter.wrap(viewName)}`, }); } diff --git a/lib/knex-builder/Knex.js b/lib/knex-builder/Knex.js index 3f89233e..dad1077f 100644 --- a/lib/knex-builder/Knex.js +++ b/lib/knex-builder/Knex.js @@ -50,7 +50,7 @@ knex.ColumnBuilder = { }, }; -knex.TableBuilder = { +knex.TableBuilder = { extend: function (methodName, fn) { TableBuilder.extend(methodName, fn); }, diff --git a/lib/migrations/migrate/MigrationGenerator.js b/lib/migrations/migrate/MigrationGenerator.js index 979510a2..bbab2e5f 100644 --- a/lib/migrations/migrate/MigrationGenerator.js +++ b/lib/migrations/migrate/MigrationGenerator.js @@ -41,7 +41,9 @@ class MigrationGenerator { _getNewMigrationName(name) { if (name[0] === '-') name = name.slice(1); - return yyyymmddhhmmss() + '_' + name + '.' + this.config.extension.split('-')[0]; + return ( + yyyymmddhhmmss() + '_' + name + '.' + this.config.extension.split('-')[0] + ); } _getNewMigrationPath(name) { diff --git a/lib/schema/builder.js b/lib/schema/builder.js index fc4fa959..82eade4a 100644 --- a/lib/schema/builder.js +++ b/lib/schema/builder.js @@ -97,9 +97,10 @@ class SchemaBuilder extends EventEmitter { }; }); - SchemaBuilder.extend = (methodName, fn) => { - if (Object.prototype.hasOwnProperty.call(SchemaBuilder.prototype, methodName)) { + if ( + Object.prototype.hasOwnProperty.call(SchemaBuilder.prototype, methodName) + ) { throw new Error( `Can't extend SchemaBuilder with existing method ('${methodName}').` ); diff --git a/lib/schema/columnbuilder.js b/lib/schema/columnbuilder.js index d520644c..f7e497ca 100644 --- a/lib/schema/columnbuilder.js +++ b/lib/schema/columnbuilder.js @@ -88,9 +88,10 @@ ColumnBuilder.prototype.notNull = ColumnBuilder.prototype.notNullable = }; }); - ColumnBuilder.extend = (methodName, fn) => { - if (Object.prototype.hasOwnProperty.call(ColumnBuilder.prototype, methodName)) { + if ( + Object.prototype.hasOwnProperty.call(ColumnBuilder.prototype, methodName) + ) { throw new Error( `Can't extend ColumnBuilder with existing method ('${methodName}').` ); diff --git a/lib/schema/tablebuilder.js b/lib/schema/tablebuilder.js index 953b09cc..99f849a1 100644 --- a/lib/schema/tablebuilder.js +++ b/lib/schema/tablebuilder.js @@ -361,9 +361,10 @@ AlterMethods.dropColumn = AlterMethods.dropColumns = function () { return this; }; - TableBuilder.extend = (methodName, fn) => { - if (Object.prototype.hasOwnProperty.call(TableBuilder.prototype, methodName)) { + if ( + Object.prototype.hasOwnProperty.call(TableBuilder.prototype, methodName) + ) { throw new Error( `Can't extend TableBuilder with existing method ('${methodName}').` ); diff --git a/lib/schema/viewbuilder.js b/lib/schema/viewbuilder.js index dd7e0e99..8ef6c1a3 100644 --- a/lib/schema/viewbuilder.js +++ b/lib/schema/viewbuilder.js @@ -79,7 +79,6 @@ const AlterMethods = { helpers.addQueryContext(ViewBuilder); - ViewBuilder.extend = (methodName, fn) => { if (Object.prototype.hasOwnProperty.call(ViewBuilder.prototype, methodName)) { throw new Error( diff --git a/package.json b/package.json index de5b97df..47d91977 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "build:ts": "tsc", "build:gitignore": "node scripts/update_gitignore_for_tsc_output.js run", "format": "prettier --write \"{lib,bin,scripts,test}/**/*.js\"", + "format:check-difference": "prettier --list-different \"{lib,bin,scripts,test}/**/*.js\"", "debug:test": "mocha --inspect-brk --exit -t 0 test/all-tests-suite.js", "debug:tape": "node --inspect-brk test/tape/index.js", "coveralls": "nyc report --reporter=lcov", - "lint": "eslint \"lib/**/*.js\" \"test/**/*.js\"", + "lint": "eslint \"lib/**/*.js\" \"test/**/*.js\" \"bin/**/*.js\"", "lint:types": "tsd && dtslint types", "lint:everything": "npm run lint:types && npm run lint", "test:unit": "npm run test:unit-only && npm run test:cli", @@ -57,7 +58,7 @@ "stress:test": "node scripts/stress-test/knex-stress-test.js | grep -A 5 -B 60 -- '- STATS '", "stress:destroy": "docker-compose -f scripts/stress-test/docker-compose.yml stop", "prepare": "husky install && npm run clean && npm run build", - "prepublishOnly": "npm run clean && npm run build" + "prepublishOnly": "npm run format:check-difference && npm run lint && npm run clean && npm run build" }, "dependencies": { "colorette": "2.0.19", @@ -114,10 +115,11 @@ "coveralls": "^3.1.1", "cross-env": "^7.0.3", "dtslint": "4.2.1", - "eslint": "^8.13.0", - "eslint-config-prettier": "^8.5.0", + "eslint": "^8.32.0", + "eslint-config-prettier": "^8.6.0", "eslint-plugin-import": "^2.26.0", "eslint-plugin-mocha-no-only": "^1.1.1", + "eslint-plugin-prettier": "^4.2.1", "husky": "^8.0.1", "jake": "^10.8.5", "JSONStream": "^1.3.5", @@ -130,7 +132,7 @@ "oracledb": "^5.4.0", "pg": "^8.8.0", "pg-query-stream": "^4.2.4", - "prettier": "2.6.2", + "prettier": "2.8.3", "rimraf": "^3.0.2", "sinon": "^15.0.1", "sinon-chai": "^3.7.0", diff --git a/scripts/clean.js b/scripts/clean.js index fa1b7884..a4dc12bb 100755 --- a/scripts/clean.js +++ b/scripts/clean.js @@ -1,29 +1,31 @@ #!/usr/bin/env node -const fs = require('fs') -const path = require('path') -const { execSync } = require("child_process"); +const fs = require('fs'); +const path = require('path'); +const { execSync } = require('child_process'); function main() { - const repoDir = path.dirname(__dirname) - const gitDir = path.join(repoDir, '.git') - const gitDirExists = doesDirectoryExist(gitDir) + const repoDir = path.dirname(__dirname); + const gitDir = path.join(repoDir, '.git'); + const gitDirExists = doesDirectoryExist(gitDir); if (!gitDirExists) { - console.log("No .git directory detected so can not clean 'lib/'. Exiting.") - process.exit(0) + console.log("No .git directory detected so can not clean 'lib/'. Exiting."); + process.exit(0); } - console.log("Cleaning 'lib/' of outputted files from Typescript compilation ...") - const cmd = 'git clean -f -X lib/' - const output = execSync(cmd, { cwd: repoDir }) - console.log(output.toString('utf8')) - console.log('Done') + console.log( + "Cleaning 'lib/' of outputted files from Typescript compilation ..." + ); + const cmd = 'git clean -f -X lib/'; + const output = execSync(cmd, { cwd: repoDir }); + console.log(output.toString('utf8')); + console.log('Done'); } function doesDirectoryExist(p) { if (fs.existsSync(p)) { - return fs.lstatSync(p).isDirectory() + return fs.lstatSync(p).isDirectory(); } - return false + return false; } -main() +main(); diff --git a/scripts/update_gitignore_for_tsc_output.js b/scripts/update_gitignore_for_tsc_output.js index 0f385d74..f48f7d57 100755 --- a/scripts/update_gitignore_for_tsc_output.js +++ b/scripts/update_gitignore_for_tsc_output.js @@ -1,12 +1,12 @@ #!/usr/bin/env node -const path = require('path') -const fs = require('fs') +const path = require('path'); +const fs = require('fs'); // Directory constants -const scriptDirectory = __dirname -const repoDirectory = path.join(scriptDirectory, '..') -const libDirectory = path.join(repoDirectory, 'lib') +const scriptDirectory = __dirname; +const repoDirectory = path.join(scriptDirectory, '..'); +const libDirectory = path.join(repoDirectory, 'lib'); const helpText = ` Helper script to update lib/.gitignore for all .js files from .ts files. @@ -19,7 +19,7 @@ Helper script to update lib/.gitignore for all .js files from .ts files. NOTES FOR USAGE: 1. This script is tested to work on Ubuntu 18.04 LTS. -` +`; const gitignoreHeader = `# DO NOT EDIT, GENERATED BY: scripts/update_gitignore_for_tsc_output.js @@ -30,57 +30,61 @@ const gitignoreHeader = `# DO NOT EDIT, GENERATED BY: scripts/update_gitignore_f **/*.js.map # Do not include .js files from .ts files -` +`; function main(cliCommand) { if (cliCommand === 'run') { - console.log('Generating lib/.gitignore ...') + console.log('Generating lib/.gitignore ...'); // Find all .ts files in lib/ - const directoriesToProcess = [libDirectory] - const tsFiles = [] + const directoriesToProcess = [libDirectory]; + const tsFiles = []; while (directoriesToProcess.length > 0) { - const directory = directoriesToProcess.pop() + const directory = directoriesToProcess.pop(); if (!fs.existsSync(directory)) { - throw new Error("Directory doesn't exist:", directory) + throw new Error("Directory doesn't exist:", directory); } - const files = fs.readdirSync(directory) + const files = fs.readdirSync(directory); files.forEach((file) => { - const filename = path.join(directory, file) - const stat = fs.lstatSync(filename) + const filename = path.join(directory, file); + const stat = fs.lstatSync(filename); if (stat.isDirectory()) { - directoriesToProcess.push(filename) + directoriesToProcess.push(filename); } else if (filename.endsWith('.ts') && !filename.endsWith('.d.ts')) { - tsFiles.push(filename) - console.log('Found .ts file:', filename) + tsFiles.push(filename); + console.log('Found .ts file:', filename); } - }) + }); } // Get paths of .js files to ignore const jsFilesToIgnore = tsFiles.map((filepath) => { // Cuts off `${libDirectory}/` - const relativeTsPath = filepath.slice(libDirectory.length + 1) + const relativeTsPath = filepath.slice(libDirectory.length + 1); // Swaps .ts for .js file ending - const relativeJsPath = relativeTsPath.slice(0, relativeTsPath.length - 3) + '.js' + const relativeJsPath = + relativeTsPath.slice(0, relativeTsPath.length - 3) + '.js'; // Always use POSIX-style path separators - .gitignore requires it return relativeJsPath.split(path.sep).join(path.posix.sep); - }) - const jsFilesToIgnoreString = jsFilesToIgnore.join('\n') - const libGitignorePath = path.join(libDirectory, '.gitignore') - fs.writeFileSync(libGitignorePath, gitignoreHeader + jsFilesToIgnoreString + '\n') - console.log('DONE') + }); + const jsFilesToIgnoreString = jsFilesToIgnore.join('\n'); + const libGitignorePath = path.join(libDirectory, '.gitignore'); + fs.writeFileSync( + libGitignorePath, + gitignoreHeader + jsFilesToIgnoreString + '\n' + ); + console.log('DONE'); } else if (['help', '--help', '-h', undefined].includes(cliCommand)) { - console.log(helpText) + console.log(helpText); } else { - console.log(`Unsupported command: ${cliCommand}`) - console.log("Try running with 'help' to see supported commands.") - process.exit(1) + console.log(`Unsupported command: ${cliCommand}`); + console.log("Try running with 'help' to see supported commands."); + process.exit(1); } } // Main script logic -const cliCommand = process.argv[2] +const cliCommand = process.argv[2]; // Start the bash app's main function -main(cliCommand) +main(cliCommand); diff --git a/test/integration/dialects/oracledb.js b/test/integration/dialects/oracledb.js index d1e175ea..3bb6d854 100644 --- a/test/integration/dialects/oracledb.js +++ b/test/integration/dialects/oracledb.js @@ -66,9 +66,9 @@ describe('Oracle', () => { const sql = compiler.insert(); expect(sql.sql).to.eql( 'begin execute immediate \'insert into "fakeTable" ("value1", "value2", "value3", "value4") values ' + - '(:1, :2, :3, :4)\' using ?, ?, ?, ?; ' + - 'execute immediate \'insert into "fakeTable" ("value1", "value2", "value3", "value4") values ' + - '(:1, DEFAULT, DEFAULT, DEFAULT)\' using ?;end;' + "(:1, :2, :3, :4)' using ?, ?, ?, ?; " + + 'execute immediate \'insert into "fakeTable" ("value1", "value2", "value3", "value4") values ' + + "(:1, DEFAULT, DEFAULT, DEFAULT)' using ?;end;" ); }); }); diff --git a/test/integration2/dialects/oracledb.spec.js b/test/integration2/dialects/oracledb.spec.js index 8d747715..ea0903b2 100644 --- a/test/integration2/dialects/oracledb.spec.js +++ b/test/integration2/dialects/oracledb.spec.js @@ -27,19 +27,18 @@ describe('Oracledb dialect', () => { describe('#4869 inserting Buffer', async () => { it('.toSQL().toNative() generates correct sql and bindings for INSERT of Buffer', async () => { - const b = Buffer.from('hello', 'utf-8') - const query = knex('table1').insert({ value: b }) - const queryObj = query.toSQL().toNative() + const b = Buffer.from('hello', 'utf-8'); + const query = knex('table1').insert({ value: b }); + const queryObj = query.toSQL().toNative(); // Ensure we have two bindings, before fix for #4869 there would only have been one due // to silent dropping of the Buffer. - expect(queryObj.bindings.length).to.eql(2) + expect(queryObj.bindings.length).to.eql(2); expect(queryObj).to.eql({ sql: 'insert into "table1" ("value") values (:1) returning "value" into :2', bindings: [b, { type: 2019, dir: 3003 }], }); }); }); - }); }); }); diff --git a/test/jake-util/helpers/migration-test-helper.js b/test/jake-util/helpers/migration-test-helper.js index 95f75850..7fcd34da 100644 --- a/test/jake-util/helpers/migration-test-helper.js +++ b/test/jake-util/helpers/migration-test-helper.js @@ -2,7 +2,7 @@ const os = require('os'); const fs = require('fs'); -const child_process = require("child_process"); +const child_process = require('child_process'); const rimrafSync = require('rimraf').sync; function assertExec(cmd, desc) { @@ -11,9 +11,7 @@ function assertExec(cmd, desc) { let stderr = ''; let stdout = ''; const bin = child_process.exec(cmd); - bin.on('error', (msg, code) => - reject(Error(desc + ' FAIL. ' + stderr)) - ); + bin.on('error', (msg, code) => reject(Error(desc + ' FAIL. ' + stderr))); bin.on('cmdEnd', (cmd) => resolve({ cmd, stdout, stderr })); bin.on('stdout', (data) => (stdout += data.toString())); bin.on('stderr', (data) => (stderr += data.toString()));