Skip to content

Commit

Permalink
fix(test-runner): add jest-matcher-utils to dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Sep 1, 2021
1 parent d8cc457 commit 1bbae1f
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 11 deletions.
102 changes: 92 additions & 10 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"expect": "^26.4.2",
"extract-zip": "^2.0.1",
"https-proxy-agent": "^5.0.0",
"jest-matcher-utils": "^26.4.2",
"jpeg-js": "^0.4.2",
"mime": "^2.4.6",
"minimatch": "^3.0.3",
Expand Down
34 changes: 33 additions & 1 deletion utils/check_deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const path = require('path');
async function checkDeps() {
const root = path.normalize(path.join(__dirname, '..'));
const src = path.normalize(path.join(__dirname, '..', 'src'));
const packageJSON = require(path.join(root, 'package.json'));
const program = ts.createProgram({
options: {
allowJs: true,
Expand All @@ -44,7 +45,7 @@ async function checkDeps() {
if (errors.length) {
console.log(`--------------------------------------------------------`);
console.log(`Changing the project structure or adding new components?`);
console.log(`Update DEPS in //${path.relative(root, __filename)}.`);
console.log(`Update DEPS in ./${path.relative(root, __filename)}`);
console.log(`--------------------------------------------------------`);
}
process.exit(errors.length ? 1 : 0);
Expand All @@ -55,6 +56,8 @@ async function checkDeps() {
const importPath = path.resolve(path.dirname(fileName), importName) + '.ts';
if (!allowImport(fileName, importPath))
errors.push(`Disallowed import from ${path.relative(root, fileName)} to ${path.relative(root, importPath)}`);
if (!alllowExternalImport(fileName, importPath, importName))
errors.push(`Disallowed external dependency ${importName} from ${path.relative(root, fileName)}`);
}
ts.forEachChild(node, x => visit(x, fileName));
}
Expand Down Expand Up @@ -91,6 +94,35 @@ async function checkDeps() {
}
return false;
}


function alllowExternalImport(from, importPath, importName) {
const EXTERNAL_IMPORT_ALLOWLIST = ['electron'];
// Only external imports are relevant. Files in src/web are bundled via webpack.
if (importName.startsWith('.') || importPath.startsWith(path.join(src, 'web')))
return true;
if (EXTERNAL_IMPORT_ALLOWLIST.includes(importName))
return true;
try {
const resolvedImport = require.resolve(importName)
const resolvedImportRelativeToNodeModules = path.relative(path.join(root, 'node_modules'), resolvedImport);
// Filter out internal Node.js modules
if (!resolvedImportRelativeToNodeModules.startsWith(importName))
return true;
const resolvedImportRelativeToNodeModulesParts = resolvedImportRelativeToNodeModules.split(path.sep);
if (packageJSON.dependencies[resolvedImportRelativeToNodeModulesParts[0]]) {
return true;
}
// handle e.g. @babel/code-frame
if (resolvedImportRelativeToNodeModulesParts.length >= 2 && packageJSON.dependencies[resolvedImportRelativeToNodeModulesParts.splice(0, 2).join(path.sep)]) {
return true;
}
return false;
} catch (error) {
if (error.code !== 'MODULE_NOT_FOUND')
throw error
}
}
}

function listAllFiles(dir) {
Expand Down

0 comments on commit 1bbae1f

Please sign in to comment.