From b0b6125844e28e2b63752b590c50cd8fad262c74 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 17 Aug 2018 13:30:00 -0700 Subject: [PATCH] [Fix] detect extraneous deps even when there are none in the first place Fixes #1161 --- src/rules/no-extraneous-dependencies.js | 6 +----- tests/src/rules/no-extraneous-dependencies.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/rules/no-extraneous-dependencies.js b/src/rules/no-extraneous-dependencies.js index 9d51018e9..ec9eedaa2 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/src/rules/no-extraneous-dependencies.js @@ -176,11 +176,7 @@ module.exports = { create: function (context) { const options = context.options[0] || {} const filename = context.getFilename() - const deps = getDependencies(context, options.packageDir) - - if (!deps) { - return {} - } + const deps = getDependencies(context, options.packageDir) || extractDepFields({}) const depsOptions = { allowDevDeps: testConfig(options.devDependencies, filename) !== false, diff --git a/tests/src/rules/no-extraneous-dependencies.js b/tests/src/rules/no-extraneous-dependencies.js index 381b392cb..10012f6b4 100644 --- a/tests/src/rules/no-extraneous-dependencies.js +++ b/tests/src/rules/no-extraneous-dependencies.js @@ -17,6 +17,7 @@ const packageFileWithSyntaxErrorMessage = (() => { const packageDirWithFlowTyped = path.join(__dirname, '../../files/with-flow-typed') const packageDirMonoRepoRoot = path.join(__dirname, '../../files/monorepo') const packageDirMonoRepoWithNested = path.join(__dirname, '../../files/monorepo/packages/nested-package') +const packageDirWithEmpty = path.join(__dirname, '../../files/empty') ruleTester.run('no-extraneous-dependencies', rule, { valid: [ @@ -263,5 +264,14 @@ ruleTester.run('no-extraneous-dependencies', rule, { message: "'react' should be listed in the project's dependencies. Run 'npm i -S react' to add it", }], }), + test({ + code: 'import "react";', + filename: path.join(packageDirWithEmpty, 'index.js'), + options: [{packageDir: packageDirWithEmpty}], + errors: [{ + ruleId: 'no-extraneous-dependencies', + message: "'react' should be listed in the project's dependencies. Run 'npm i -S react' to add it", + }], + }), ] })