From 26871a7debff14cb10f74917f70d72eef2965243 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 15 Aug 2024 15:48:39 +0100 Subject: [PATCH] Disallowing access to the backend-core barrel file as it can cause problems, it should only ever be imported externally via @budibase/backend-core. --- .eslintrc.json | 1 + eslint-local-rules/index.js | 56 ++++++++++++++++++- .../src/context/tests/index.spec.ts | 2 +- .../src/features/tests/features.spec.ts | 2 +- .../src/redis/tests/redis.spec.ts | 2 +- 5 files changed, 57 insertions(+), 6 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index f614f1ad91..a94ea87704 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -43,6 +43,7 @@ }, "rules": { "no-unused-vars": "off", + "local-rules/no-barrel-imports": "error", "local-rules/no-budibase-imports": "error", "local-rules/no-console-error": "error", "@typescript-eslint/no-unused-vars": [ diff --git a/eslint-local-rules/index.js b/eslint-local-rules/index.js index e88642c905..d9d894c33e 100644 --- a/eslint-local-rules/index.js +++ b/eslint-local-rules/index.js @@ -1,6 +1,17 @@ +const path = require("path") + +const makeBarrelPath = finalPath => { + return path.resolve(__dirname, "..", finalPath) +} +const backendCoreBarrelPaths = [ + makeBarrelPath(path.join("packages", "backend-core", "src", "index.ts")), + makeBarrelPath(path.join("packages", "backend-core", "src")), + makeBarrelPath(path.join("packages", "backend-core")), +] + module.exports = { "no-console-error": { - create: function(context) { + create: function (context) { return { CallExpression(node) { if ( @@ -13,11 +24,12 @@ module.exports = { ) { context.report({ node, - message: 'Using console.error(err) on its own is not allowed. Either provide context to the error (console.error(msg, err)) or throw it.', + message: + "Using console.error(err) on its own is not allowed. Either provide context to the error (console.error(msg, err)) or throw it.", }) } }, - }; + } }, }, "no-budibase-imports": { @@ -106,4 +118,42 @@ module.exports = { } }, }, + "no-barrel-imports": { + meta: { + type: "problem", + docs: { + description: + "Disallow imports from the top-level backend-core barrel file", + category: "Best Practices", + recommended: false, + }, + schema: [], // no options + messages: { + noBarrelImport: + "Avoid importing from the top-level barrel file 'backend-core/src/index.ts'. Import directly from the specific module instead.", + }, + }, + create(context) { + return { + ImportDeclaration(node) { + const importPath = node.source.value + const importFullPath = path.resolve( + context.getFilename(), + "..", + importPath + ) + + if (backendCoreBarrelPaths.includes(importFullPath)) { + context.report({ + node, + messageId: "noBarrelImport", + data: { + importFullPath, + }, + }) + } + }, + } + }, + }, } diff --git a/packages/backend-core/src/context/tests/index.spec.ts b/packages/backend-core/src/context/tests/index.spec.ts index 2d89131549..95eb635ce7 100644 --- a/packages/backend-core/src/context/tests/index.spec.ts +++ b/packages/backend-core/src/context/tests/index.spec.ts @@ -2,7 +2,7 @@ import { testEnv } from "../../../tests/extra" import * as context from "../" import { DEFAULT_TENANT_ID } from "../../constants" import { structures } from "../../../tests" -import { db } from "../.." +import * as db from "../../db" import Context from "../Context" import { ContextMap } from "../types" import { IdentityType } from "@budibase/types" diff --git a/packages/backend-core/src/features/tests/features.spec.ts b/packages/backend-core/src/features/tests/features.spec.ts index 1e8e25654a..553558ab14 100644 --- a/packages/backend-core/src/features/tests/features.spec.ts +++ b/packages/backend-core/src/features/tests/features.spec.ts @@ -1,6 +1,6 @@ import { IdentityContext, IdentityType, UserCtx } from "@budibase/types" import { Flag, FlagSet, FlagValues, init, shutdown } from "../" -import { context } from "../.." +import * as context from "../../context" import environment, { withEnv } from "../../environment" import nodeFetch from "node-fetch" import nock from "nock" diff --git a/packages/backend-core/src/redis/tests/redis.spec.ts b/packages/backend-core/src/redis/tests/redis.spec.ts index 376afbfab7..4ea41fdb7f 100644 --- a/packages/backend-core/src/redis/tests/redis.spec.ts +++ b/packages/backend-core/src/redis/tests/redis.spec.ts @@ -1,7 +1,7 @@ import { GenericContainer, StartedTestContainer } from "testcontainers" import { generator, structures } from "../../../tests" import RedisWrapper, { closeAll } from "../redis" -import { env } from "../.." +import env from "../../environment" import { randomUUID } from "crypto" jest.setTimeout(30000)