budibase/eslint-local-rules/index.js

160 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

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")),
]
2023-11-17 14:39:52 +01:00
module.exports = {
2024-04-10 18:27:15 +02:00
"no-console-error": {
create: function (context) {
2024-04-10 18:27:15 +02:00
return {
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.object.name === "console" &&
node.callee.property.name === "error" &&
node.arguments.length === 1 &&
2024-04-10 18:39:12 +02:00
node.arguments[0].name &&
2024-04-10 18:27:15 +02:00
node.arguments[0].name.startsWith("err")
) {
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.",
2024-04-10 18:27:15 +02:00
})
}
},
}
2024-04-10 18:27:15 +02:00
},
},
2023-11-17 14:39:52 +01:00
"no-budibase-imports": {
create: function (context) {
return {
ImportDeclaration(node) {
const importPath = node.source.value
if (
/^@budibase\/[^/]+\/.*$/.test(importPath) &&
2024-03-15 16:40:30 +01:00
importPath !== "@budibase/backend-core/tests" &&
importPath !== "@budibase/string-templates/test/utils"
2023-11-17 14:39:52 +01:00
) {
context.report({
node,
2024-03-15 16:40:30 +01:00
message: `Importing from @budibase is not allowed, except for @budibase/backend-core/tests and @budibase/string-templates/test/utils.`,
2023-11-17 14:39:52 +01:00
})
}
},
}
},
},
"no-test-com": {
meta: {
type: "problem",
docs: {
description:
"disallow the use of 'test.com' in strings and replace it with 'example.com'",
},
schema: [],
fixable: "code",
},
create: function (context) {
return {
Literal(node) {
if (
typeof node.value === "string" &&
node.value.includes("test.com")
) {
context.report({
node,
message:
"test.com is a privately owned domain and could point anywhere, use example.com instead.",
fix: function (fixer) {
const newText = node.raw.replace(/test\.com/g, "example.com")
return fixer.replaceText(node, newText)
},
})
}
},
}
},
},
"email-domain-example-com": {
meta: {
type: "problem",
docs: {
description:
"enforce using the example.com domain for generator.email calls",
},
fixable: "code",
schema: [],
},
create: function (context) {
return {
CallExpression(node) {
if (
node.callee.type === "MemberExpression" &&
node.callee.object.name === "generator" &&
node.callee.property.name === "email" &&
node.arguments.length === 0
) {
context.report({
node,
message:
"Prefer using generator.email with the domain \"{ domain: 'example.com' }\".",
fix: function (fixer) {
return fixer.replaceText(
node,
'generator.email({ domain: "example.com" })'
)
},
})
}
},
}
},
},
"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,
},
})
}
},
}
},
},
2023-11-17 14:39:52 +01:00
}