Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust tests for 6da99130 #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/core/packagePath.js
Expand Up @@ -13,6 +13,10 @@ export function getFilePackagePath(filePath) {
}

export function getFilePackageName(filePath) {
const { pkg } = readPkgUp.sync({ cwd: filePath, normalize: false });
return pkg && pkg.name;
const { pkg, path } = readPkgUp.sync({ cwd: filePath, normalize: false });
if (pkg) {
// recursion in case of intermediate esm package.json without name found
return pkg.name || getFilePackageName(dirname(dirname(path)));
}
return null;
}
7 changes: 7 additions & 0 deletions src/rules/no-extraneous-dependencies.js
Expand Up @@ -184,8 +184,15 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
// test the real name from the resolved package.json
// if not aliased imports (alias/react for example), importPackageName can be misinterpreted
const realPackageName = getModuleRealName(resolved);

if(!realPackageName){
throw new Error(`cant find real package name for import ${name}`);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of defaulting to an empty package name (which is what line 133 effectively did), throw an error when the package name is not found. This will make it easier to find bugs like this in the future.

const realPackageNameDeclaration = checkDependencyDeclaration(deps, realPackageName);



if (realPackageNameDeclaration.isInDeps ||
(depsOptions.allowDevDeps && realPackageNameDeclaration.isInDevDeps) ||
(depsOptions.allowPeerDeps && realPackageNameDeclaration.isInPeerDeps) ||
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions tests/files/node_modules/esm-package/esm-module/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
5 changes: 5 additions & 0 deletions tests/files/node_modules/esm-package/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/src/rules/no-extraneous-dependencies.js
Expand Up @@ -154,6 +154,7 @@ ruleTester.run('no-extraneous-dependencies', rule, {
test({
code: 'import "rxjs/operators"',
}),

],
invalid: [
test({
Expand Down Expand Up @@ -358,6 +359,12 @@ ruleTester.run('no-extraneous-dependencies', rule, {
message: `'not-a-dependency' should be listed in the project's dependencies. Run 'npm i -S not-a-dependency' to add it`,
}],
}),
test({
code: 'import "esm-package/esm-module";',
errors: [{
message: `'esm-package' should be listed in the project's dependencies. Run 'npm i -S esm-package' to add it`,
}],
}),
],
});

Expand Down