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

Improve S4328 (no-implicit-dependencies): check package.json files in all levels up #3102

Merged
merged 5 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 45 additions & 34 deletions eslint-bridge/src/rules/no-implicit-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ import { RequiredParserServices } from '../utils';
const DefinitelyTyped = '@types/';

/**
* Cache for each dirname the dependencies of the nearest package.json.
* Cache for each dirname the dependencies of the package.json in this directory, empty set when no package.json.
*/
const dirCache: Map<string, Set<string>> = new Map();

/**
* Cache for the available dependencies by dirname.
*/
const cache: Map<string, Set<string>> = new Map();

Expand Down Expand Up @@ -146,33 +151,54 @@ function getPackageName(name: string) {
}

function getDependencies(fileName: string) {
const dirname = path.dirname(fileName);

let dirname = path.dirname(fileName);
const cached = cache.get(dirname);
if (cached) {
return cached;
}

const result = new Set<string>();
const packageJsonPath = findPackageJson(path.resolve(dirname));
if (packageJsonPath !== undefined) {
try {
// remove BOM from file content before parsing
const content = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8').replace(/^\uFEFF/, ''));
if (content.dependencies !== undefined) {
addDependencies(result, content.dependencies);
}
if (content.devDependencies !== undefined) {
addDependencies(result, content.devDependencies);
}
if (content.peerDependencies !== undefined) {
addDependencies(result, content.peerDependencies);
}
} catch {}
cache.set(dirname, result);

while (true) {
const dirCached = dirCache.get(dirname);
if (dirCached) {
dirCached.forEach(d => result.add(d));
} else {
const packageJsonPath = path.join(path.resolve(dirname), 'package.json');
const dep = fs.existsSync(packageJsonPath)
? getDependenciesFromPackageJson(packageJsonPath)
: new Set<string>();
dep.forEach(d => result.add(d));
dirCache.set(dirname, dep);
}

const upperDir = path.dirname(dirname);
if (upperDir === dirname) {
break;
} else {
dirname = upperDir;
}
Comment on lines +176 to +181

Choose a reason for hiding this comment

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

Food for thought...

Just saying, and not sure if it would make the code much more readable, but we could get rid of the if-break-else thing by turning the infinite while loop into a do-while loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see the point but not sure it's clearer with do-while

}

cache.set(dirname, result);
return result;
}

function getDependenciesFromPackageJson(packageJsonPath: string) {
const result = new Set<string>();
try {
// remove BOM from file content before parsing
const content = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8').replace(/^\uFEFF/, ''));
if (content.dependencies !== undefined) {
addDependencies(result, content.dependencies);
}
if (content.devDependencies !== undefined) {
addDependencies(result, content.devDependencies);
}
if (content.peerDependencies !== undefined) {
addDependencies(result, content.peerDependencies);
}
} catch {}
return result;
}

Expand All @@ -182,21 +208,6 @@ function addDependencies(result: Set<string>, dependencies: any) {
);
}

function findPackageJson(current: string): string | undefined {
const fileName = path.join(current, 'package.json');
if (fs.existsSync(fileName)) {
return fileName;
}

const prev: string = current;
current = path.dirname(current);

if (prev !== current) {
return findPackageJson(current);
}
return undefined;
}

/**
* The matching pattern part of a path mapping specified
* in `paths` in `tsconfig.json`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"nested-dependency": "latest"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"top-dependency": "latest"
}
}
31 changes: 31 additions & 0 deletions eslint-bridge/tests/rules/no-implicit-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,37 @@ ruleTester.run('Dependencies should be explicit', rule, {
],
});

const ruleTesterNestedPackage = new RuleTester({
parser: tsParserPath,
parserOptions: { ecmaVersion: 2018, sourceType: 'module' },
});

const filenameNestedPackage = path.join(
__dirname,
'../fixtures/no-implicit-dependencies/nested-package-json-project/dir/file.js',
);

ruleTesterNestedPackage.run('all levels of package.json should be considered', rule, {
valid: [
{
code: `
import { f as f1 } from 'top-dependency';
import { f as f2 } from 'nested-dependency';
`,
filename: filenameNestedPackage,
},
],
invalid: [
{
code: `
import { f as f1 } from 'nonexistent';
`,
filename: filenameNestedPackage,
errors: 1,
},
],
});

const ruleTesterForPathMappings = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
Expand Down
74 changes: 0 additions & 74 deletions its/ruling/src/test/expected/ts/desktop/typescript-S4328.json

This file was deleted.