diff --git a/eslint.config.js b/eslint.config.js index ff4047348b..13e04a79b5 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -46,7 +46,12 @@ module.exports = [ }, ], 'import-x/no-unresolved': ['error', { commonjs: true }], - 'no-unused-vars': ['error', { caughtErrors: 'none' }], + 'no-unused-vars': [ + 'error', + { + caughtErrors: 'all', + }, + ], 'n/no-unsupported-features/node-builtins': ['error', { allowExperimental: true }], 'n/no-extraneous-require': 'off', 'n/no-unpublished-require': 'off', @@ -56,6 +61,12 @@ module.exports = [ 'n/hashbang': 'off', }, }, + { + files: ['bin/serverless.js', 'commands/**/*.js', 'lib/**/*.js', 'scripts/serverless.js'], + rules: { + 'n/no-unpublished-require': 'error', + }, + }, { files: ['**/*.mjs'], languageOptions: { diff --git a/lib/cli/run-compose.js b/lib/cli/run-compose.js index 547ba1514e..3413ae8289 100644 --- a/lib/cli/run-compose.js +++ b/lib/cli/run-compose.js @@ -28,7 +28,7 @@ const resolveGlobalNpmPath = async () => { const npmNodeModulesPath = await (async () => { try { return String((await spawn('npm', ['root', '-g'])).stdoutBuffer).trim(); - } catch (error) { + } catch { return null; } })(); @@ -36,7 +36,7 @@ const resolveGlobalNpmPath = async () => { if (!npmNodeModulesPath) return null; try { return require.resolve(`${npmNodeModulesPath}/${relativeBinPath}`); - } catch (globalDepError) { + } catch { return null; } }; diff --git a/lib/configuration/variables/resolve.js b/lib/configuration/variables/resolve.js index 0225286e8d..2ff0d9c6b2 100644 --- a/lib/configuration/variables/resolve.js +++ b/lib/configuration/variables/resolve.js @@ -648,7 +648,7 @@ Object.defineProperties( let normalizedResultValue; try { normalizedResultValue = JSON.parse(JSON.stringify(resultValue)); - } catch (error) { + } catch { throw new VariableSourceResolutionError( `Source "${sourceData.type}" returned not supported result: "${util.inspect( resultValue diff --git a/lib/configuration/variables/sources/file.js b/lib/configuration/variables/sources/file.js index 9d7418af39..a7e0459e01 100644 --- a/lib/configuration/variables/sources/file.js +++ b/lib/configuration/variables/sources/file.js @@ -93,7 +93,7 @@ module.exports = { case '.cjs': { try { require.resolve(filePath); - } catch (error) { + } catch { return null; } let result; diff --git a/lib/plugins/aws/deploy/lib/ensure-valid-bucket-exists.js b/lib/plugins/aws/deploy/lib/ensure-valid-bucket-exists.js index 5d1f33a1b7..bb7acc2df6 100644 --- a/lib/plugins/aws/deploy/lib/ensure-valid-bucket-exists.js +++ b/lib/plugins/aws/deploy/lib/ensure-valid-bucket-exists.js @@ -80,10 +80,10 @@ module.exports = { if (getTemplateResult.TemplateBody) { try { templateBody = JSON.parse(getTemplateResult.TemplateBody); - } catch (error) { + } catch { try { templateBody = jsyaml.load(getTemplateResult.TemplateBody); - } catch (error2) { + } catch { throw new ServerlessError( 'Could not parse CloudFormation template', 'CLOUDFORMATION_TEMPLATE_PARSE_FAILED' diff --git a/lib/plugins/aws/invoke-local/index.js b/lib/plugins/aws/invoke-local/index.js index 57f185f72d..2e3d6140f2 100644 --- a/lib/plugins/aws/invoke-local/index.js +++ b/lib/plugins/aws/invoke-local/index.js @@ -135,7 +135,7 @@ class AwsInvokeLocal { this.options.context = JSON.parse(this.options.context); } } - } catch (exception) { + } catch { // do nothing if it's a simple string or object already } } @@ -704,7 +704,7 @@ class AwsInvokeLocal { try { await fsp.stat(executablePath); - } catch (err) { + } catch { return new Promise((resolve, reject) => { const javaBridgeProgress = progress.get('javaBridge'); javaBridgeProgress.notice('Building Java bridge', { isMainEvent: true }); @@ -883,7 +883,7 @@ class AwsInvokeLocal { } try { JSON.stringify(result); - } catch (error) { + } catch { throw new ServerlessError( `Function returned invalid (not a JSON stringifiable) value: ${inspect(result)}`, 'INVALID_INVOKE_LOCAL_RESULT' @@ -896,7 +896,7 @@ class AwsInvokeLocal { Object.assign(result, { body: JSON.parse(result.body), }); - } catch (e) { + } catch { throw new ServerlessError( 'Content-Type of response is application/json but body is not json', 'INVOKE_LOCAL_RESPONSE_TYPE_MISMATCH' diff --git a/lib/plugins/aws/invoke.js b/lib/plugins/aws/invoke.js index ec76cc8bd9..0bf9e01444 100644 --- a/lib/plugins/aws/invoke.js +++ b/lib/plugins/aws/invoke.js @@ -61,7 +61,7 @@ class AwsInvoke { if (!this.options.raw) { this.options.data = JSON.parse(this.options.data); } - } catch (exception) { + } catch { // do nothing if it's a simple string or object already } @@ -69,7 +69,7 @@ class AwsInvoke { if (!this.options.raw && this.options.context) { this.options.context = JSON.parse(this.options.context); } - } catch (exception) { + } catch { // do nothing if it's a simple string or object already } } diff --git a/lib/plugins/aws/provider.js b/lib/plugins/aws/provider.js index 3d68cbc1a4..761480077a 100644 --- a/lib/plugins/aws/provider.js +++ b/lib/plugins/aws/provider.js @@ -2299,7 +2299,7 @@ Object.defineProperties( async () => { try { await spawnExt('docker', ['--version']); - } catch (err) { + } catch { throw new ServerlessError( 'Could not find Docker installation. Ensure Docker is installed before continuing.', 'DOCKER_COMMAND_NOT_AVAILABLE' diff --git a/lib/plugins/package/lib/zip-service.js b/lib/plugins/package/lib/zip-service.js index e39f8a3f65..85f138e5da 100644 --- a/lib/plugins/package/lib/zip-service.js +++ b/lib/plugins/package/lib/zip-service.js @@ -288,7 +288,7 @@ async function excludeNodeDevDependencies(serviceDir) { fsp.unlink(nodeProdDepFile).catch(() => {}), ]); return exAndIn; - } catch (e) { + } catch { // fail silently return exAndIn; } diff --git a/lib/utils/fs/dir-exists-sync.js b/lib/utils/fs/dir-exists-sync.js index 9d4d1760ea..84105e4ffc 100644 --- a/lib/utils/fs/dir-exists-sync.js +++ b/lib/utils/fs/dir-exists-sync.js @@ -6,7 +6,7 @@ function dirExistsSync(dirPath) { try { const stats = fse.statSync(dirPath); return stats.isDirectory(); - } catch (e) { + } catch { return false; } } diff --git a/lib/utils/fs/file-exists-sync.js b/lib/utils/fs/file-exists-sync.js index 95b9295092..fb793e6321 100644 --- a/lib/utils/fs/file-exists-sync.js +++ b/lib/utils/fs/file-exists-sync.js @@ -6,7 +6,7 @@ function fileExistsSync(filePath) { try { const stats = fse.statSync(filePath); return stats.isFile(); - } catch (e) { + } catch { return false; } } diff --git a/test/fixtures/programmatic/plugin/extend-config-plugin/index.js b/test/fixtures/programmatic/plugin/extend-config-plugin/index.js index 1dcc8a6b0a..62cf4faac0 100644 --- a/test/fixtures/programmatic/plugin/extend-config-plugin/index.js +++ b/test/fixtures/programmatic/plugin/extend-config-plugin/index.js @@ -28,13 +28,13 @@ module.exports = class TestPlugin { try { this.serverless.extendConfiguration([], { custom: {} }); - } catch (error) { + } catch { // ignore this } try { this.serverless.extendConfiguration('custom.target.invalid', {}); - } catch (error) { + } catch { // ignore this } } @@ -42,7 +42,7 @@ module.exports = class TestPlugin { extendAfterInit() { try { this.serverless.extendConfiguration(pluginConfig.afterInitValuePath, 'value'); - } catch (error) { + } catch { // ignore this } } diff --git a/test/integration/aws/websocket.test.js b/test/integration/aws/websocket.test.js index 4fb11c62a0..37ea2a1cb5 100644 --- a/test/integration/aws/websocket.test.js +++ b/test/integration/aws/websocket.test.js @@ -59,7 +59,7 @@ describe('AWS - API Gateway Websocket Integration Test', function () { promiseReject(error); try { ws.close(); - } catch (closeError) { + } catch { // safe to ignore } })(reject); @@ -102,7 +102,7 @@ describe('AWS - API Gateway Websocket Integration Test', function () { promiseReject(error); try { ws.close(); - } catch (closeError) { + } catch { // safe to ignore } })(reject); diff --git a/test/lib/setup-fixtures-engine.js b/test/lib/setup-fixtures-engine.js index 24430dfcbb..8221389a01 100644 --- a/test/lib/setup-fixtures-engine.js +++ b/test/lib/setup-fixtures-engine.js @@ -118,7 +118,7 @@ module.exports = memoizee((fixturesPath) => { (() => { try { return loadYaml(configContent, { schema: cloudformationSchema }); - } catch (error) { + } catch { return null; } })(); diff --git a/test/unit/lib/classes/plugin-manager.test.js b/test/unit/lib/classes/plugin-manager.test.js index 34f1002e55..407faa450b 100644 --- a/test/unit/lib/classes/plugin-manager.test.js +++ b/test/unit/lib/classes/plugin-manager.test.js @@ -1614,7 +1614,7 @@ describe('PluginManager', () => { process.chdir(cwd); try { fse.removeSync(tmpDir); - } catch (e) { + } catch { // Couldn't delete temporary file } }); diff --git a/test/unit/lib/config-schema.test.js b/test/unit/lib/config-schema.test.js index ef99bdcabe..baf8daec3d 100644 --- a/test/unit/lib/config-schema.test.js +++ b/test/unit/lib/config-schema.test.js @@ -101,7 +101,7 @@ describe('test/unit/lib/configSchema.test.js', () => { (err) => { try { expect(err.message).to.include(someCase.errorMessage); - } catch (error) { + } catch { throw err; } } diff --git a/test/unit/lib/plugins/aws/invoke-local/index.test.js b/test/unit/lib/plugins/aws/invoke-local/index.test.js index 72abc1de0f..6e0dc797f9 100644 --- a/test/unit/lib/plugins/aws/invoke-local/index.test.js +++ b/test/unit/lib/plugins/aws/invoke-local/index.test.js @@ -1789,7 +1789,7 @@ describe('test/unit/lib/plugins/aws/invokeLocal/index.test.js', () => { const executable = process.platform === 'win32' ? 'python.exe' : 'python'; try { await spawnExt(executable, ['--version']); - } catch (err) { + } catch { skipWithNotice(this, 'Python runtime is not installed'); } }); @@ -1815,7 +1815,7 @@ describe('test/unit/lib/plugins/aws/invokeLocal/index.test.js', () => { const executable = process.platform === 'win32' ? 'ruby.exe' : 'ruby'; try { await spawnExt(executable, ['--version']); - } catch (err) { + } catch { skipWithNotice(this, 'Ruby runtime is not installed'); } }); diff --git a/test/utils/aws-cleanup.js b/test/utils/aws-cleanup.js index bbb4beff1c..c0da638b43 100644 --- a/test/utils/aws-cleanup.js +++ b/test/utils/aws-cleanup.js @@ -62,7 +62,7 @@ async function cleanup() { const promises = bucketsToRemove.map((bucket) => deleteBucket(bucket.PhysicalResourceId)); try { await Promise.all(promises); - } catch (error) { + } catch { // do nothing... try to continue with cleanup } } @@ -72,7 +72,7 @@ async function cleanup() { const promises = stacksToRemove.map((stack) => deleteStack(stack.StackName)); try { await Promise.all(promises); - } catch (error) { + } catch { // do nothing... try to continue with cleanup } } @@ -82,7 +82,7 @@ async function cleanup() { const promises = apisToRemove.map((api) => deleteRestApi(api.id)); try { await Promise.all(promises); - } catch (error) { + } catch { // do nothing... try to continue with cleanup } } @@ -92,7 +92,7 @@ async function cleanup() { const promises = userPoolsToRemove.map((userPool) => deleteUserPoolById(userPool.Id)); try { await Promise.all(promises); - } catch (error) { + } catch { // do nothing... try to continue with cleanup } } diff --git a/test/utils/integration.js b/test/utils/integration.js index ac42a75e1e..2221840f14 100644 --- a/test/utils/integration.js +++ b/test/utils/integration.js @@ -27,7 +27,7 @@ async function resolveServiceName(cwd) { const configObject = (() => { try { return loadYaml(configContent); - } catch (error) { + } catch { return null; } })();