Skip to content

Commit 81cdd65

Browse files
authored
fix: Forbid @deephaven modules from self importing (#1499)
- Eslint rules to forbid importing from @deephaven package owning the module - Fixed offending SessionUtils I tested this by running npm run test:lint. It showed expected linting error before fixing SessionUtils. Fixing SessionUtils made error go away. Also tested in vscode. Error shows up in the editor as expected. fixes #1497
1 parent 93687a7 commit 81cdd65

3 files changed

Lines changed: 56 additions & 4 deletions

File tree

.eslintrc.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const buildPackageManifest = require('./packageManifest');
2+
3+
const { packageNames, packageManifest } = buildPackageManifest();
4+
15
module.exports = {
26
root: true,
37
extends: ['@deephaven/eslint-config'],
@@ -10,5 +14,18 @@ module.exports = {
1014
tsconfigRootDir: __dirname,
1115
},
1216
},
17+
// For each @deephaven package, forbid importing from itself
18+
...packageNames.map(packageName => ({
19+
files: [`packages/${packageManifest.get(packageName)}/**/*.@(ts|tsx)`],
20+
rules: {
21+
'no-restricted-imports': [
22+
'error',
23+
{
24+
name: packageName,
25+
message: 'Forbid importing from owning @deephaven package.',
26+
},
27+
],
28+
},
29+
})),
1330
],
1431
};

packageManifest.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
/**
5+
* Build a package manifest for the @deephaven packages.
6+
* @returns {{
7+
* packageNames: string[], // The package names.
8+
* packageManifest: Map<string, string> // Map of package names to directory names.
9+
* }}
10+
*/
11+
function buildPackageManifest() {
12+
const packageDirNames = fs
13+
.readdirSync(path.join(__dirname, 'packages'), { withFileTypes: true })
14+
.filter(ent => ent.isDirectory())
15+
.map(({ name }) => name);
16+
17+
// Map of package name to directory name
18+
const packageManifest = new Map();
19+
20+
packageDirNames.forEach(dirName => {
21+
// eslint-disable-next-line import/no-dynamic-require, global-require
22+
const packageName = require(path.join(
23+
__dirname,
24+
'packages',
25+
dirName,
26+
'package.json'
27+
)).name;
28+
29+
packageManifest.set(packageName, dirName);
30+
});
31+
32+
return {
33+
packageNames: [...packageManifest.keys()],
34+
packageManifest,
35+
};
36+
}
37+
38+
module.exports = buildPackageManifest;

packages/jsapi-utils/src/SessionUtils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ import type {
55
IdeConnection,
66
IdeSession,
77
} from '@deephaven/jsapi-types';
8-
import {
9-
requestParentResponse,
10-
SESSION_DETAILS_REQUEST,
11-
} from '@deephaven/jsapi-utils';
128
import Log from '@deephaven/log';
139
import shortid from 'shortid';
10+
import { requestParentResponse, SESSION_DETAILS_REQUEST } from './MessageUtils';
1411
import NoConsolesError, { isNoConsolesError } from './NoConsolesError';
1512

1613
const log = Log.module('SessionUtils');

0 commit comments

Comments
 (0)