chore: improve check-deps (#3592)

This commit is contained in:
Dmitry Gozman 2020-08-23 21:24:16 -07:00 committed by GitHub
parent 73e53b21df
commit 43893cc0f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -42,7 +42,7 @@ async function checkDeps() {
function visit(node, fileName) { function visit(node, fileName) {
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) { if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
const importName = node.moduleSpecifier.text; const importName = node.moduleSpecifier.text;
const importPath = path.resolve(path.dirname(fileName), importName); const importPath = path.resolve(path.dirname(fileName), importName) + '.ts';
if (!allowImport(fileName, importPath)) if (!allowImport(fileName, importPath))
errors.push(`Disallowed import from ${path.relative(root, fileName)} to ${path.relative(root, importPath)}`); errors.push(`Disallowed import from ${path.relative(root, fileName)} to ${path.relative(root, importPath)}`);
} }
@ -51,37 +51,61 @@ async function checkDeps() {
function allowImport(from, to) { function allowImport(from, to) {
from = from.substring(from.indexOf('src' + path.sep)).replace(/\\/g, '/'); from = from.substring(from.indexOf('src' + path.sep)).replace(/\\/g, '/');
to = to.substring(to.indexOf('src' + path.sep)).replace(/\\/g, '/') + '.ts'; to = to.substring(to.indexOf('src' + path.sep)).replace(/\\/g, '/');
const fromDirectory = from.substring(0, from.lastIndexOf('/') + 1);
const toDirectory = to.substring(0, to.lastIndexOf('/') + 1); const toDirectory = to.substring(0, to.lastIndexOf('/') + 1);
while (from.lastIndexOf('/') !== -1) { if (fromDirectory === toDirectory)
from = from.substring(0, from.lastIndexOf('/')); return true;
const allowed = DEPS.get(from + '/');
if (!allowed) if (['src/', 'src/rpc/server/', 'src/rpc/'].includes(fromDirectory))
continue; return true; // Temporary.
for (const dep of allowed) { if (toDirectory === 'src/')
if (to === dep || toDirectory === dep ) return true; // Temporary.
while (!DEPS[from]) {
if (from.endsWith('/'))
from = from.substring(0, from.length - 1);
if (from.lastIndexOf('/') === -1)
break;
from = from.substring(0, from.lastIndexOf('/') + 1);
}
const deps = DEPS[from] || [`+${fromDirectory}`];
for (const dep of deps) {
if (to === dep || toDirectory === dep)
return true;
if (dep.endsWith('**')) {
const parent = dep.substring(0, dep.length - 2);
if (to.startsWith(parent))
return true; return true;
} }
return false;
} }
// Allow everything else for now. return false;
return true;
} }
} }
const DEPS = new Map([ const DEPS = {};
['src/utils/', ['src/utils/']],
['src/common/', ['src/common/']], // No deps for code shared between node and page.
['src/protocol/', ['src/protocol/', 'src/utils/']], DEPS['src/common/'] = [];
['src/install/', ['src/install/', 'src/utils/']],
['src/server/debug/', ['src/server/debug/', 'src/common/', 'src/server/injected/', 'src/', 'src/server/debug/injected/']], DEPS['src/protocol/'] = ['src/utils/'];
// TODO: reverse the injected->types dependency. DEPS['src/install/'] = ['src/utils/'];
['src/server/injected/', ['src/server/injected/', 'src/common/', 'src/types.ts']],
['src/server/chromium/', ['src/server/chromium/', 'src/utils/', 'src/', 'src/common/', 'src/server/']], DEPS['src/client/'] = ['src/utils/', 'src/protocol/', 'src/server/chromium/protocol.ts'];
['src/server/electron/', ['src/server/electron/', 'src/server/chromium/', 'src/utils/', 'src/', 'src/server/']],
['src/server/firefox/', ['src/server/firefox/', 'src/utils/', 'src/', 'src/common/', 'src/server/']], DEPS['src/server/'] = ['src/utils/', 'src/common/', 'src/server/injected/'];
['src/server/webkit/', ['src/server/webkit/', 'src/utils/', 'src/', 'src/common/', 'src/server/']],
['src/client/', ['src/client/', 'src/utils/', 'src/protocol/', 'src/server/chromium/protocol.ts']], // Strict deps for injected code.
]); // TODO: reverse the injected->types dependency.
DEPS['src/server/injected/'] = ['src/common/', 'src/types.ts'];
DEPS['src/server/debug/'] = [...DEPS['src/server/'], 'src/server/debug/**'];
DEPS['src/server/chromium/'] = [...DEPS['src/server/'], 'src/server/'];
DEPS['src/server/electron/'] = [...DEPS['src/server/'], 'src/server/', 'src/server/chromium/'];
DEPS['src/server/firefox/'] = [...DEPS['src/server/'], 'src/server/'];
DEPS['src/server/webkit/'] = [...DEPS['src/server/'], 'src/server/'];
DEPS['src/server/playwright.ts'] = [...DEPS['src/server/'], 'src/server/chromium/', 'src/server/webkit/', 'src/server/firefox/'];
checkDeps(); checkDeps();