Skip to content

Instantly share code, notes, and snippets.

@johnsoncodehk
Created April 23, 2025 13:50
Show Gist options
  • Save johnsoncodehk/ef70fd4be971e0cd322394add6a8a9b7 to your computer and use it in GitHub Desktop.
Save johnsoncodehk/ef70fd4be971e0cd322394add6a8a9b7 to your computer and use it in GitHub Desktop.
import type config = require('@tsslint/config');
import path = require('node:path');
export function create(): config.Rule {
return ({ typescript: ts, sourceFile, reportError, languageServiceHost }) => {
const { noEmit } = languageServiceHost.getCompilationSettings();
if (noEmit) {
return;
}
const packageJsonPath = ts.findConfigFile(sourceFile.fileName, ts.sys.fileExists, 'package.json');
if (!packageJsonPath) {
return;
}
const packageJson = JSON.parse(ts.sys.readFile(packageJsonPath) ?? '');
if (packageJson.private) {
return;
}
const parentPackageJsonPath = ts.findConfigFile(path.dirname(path.dirname(packageJsonPath)), ts.sys.fileExists, 'package.json');
const parentPackageJson = !!parentPackageJsonPath && parentPackageJsonPath !== packageJsonPath
? JSON.parse(ts.sys.readFile(parentPackageJsonPath) ?? '')
: {};
ts.forEachChild(sourceFile, function visit(node) {
if (
ts.isImportDeclaration(node)
&& !node.importClause?.isTypeOnly
&& ts.isStringLiteral(node.moduleSpecifier)
&& !node.moduleSpecifier.text.startsWith('./')
&& !node.moduleSpecifier.text.startsWith('../')
) {
let moduleName = node.moduleSpecifier.text.split('/')[0];
if (moduleName.startsWith('@')) {
moduleName += '/' + node.moduleSpecifier.text.split('/')[1];
}
if (
(
packageJson.devDependencies?.[moduleName]
|| parentPackageJson.dependencies?.[moduleName]
|| parentPackageJson.devDependencies?.[moduleName]
|| parentPackageJson.peerDependencies?.[moduleName]
)
&& !packageJson.dependencies?.[moduleName]
&& !packageJson.peerDependencies?.[moduleName]
) {
reportError(
`Module '${moduleName}' should be in the dependencies.`,
node.getStart(sourceFile),
node.getEnd()
);
}
}
ts.forEachChild(node, visit);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment