eslint-plugin-firebase-functions
Version:
82 lines (78 loc) • 2.62 kB
JavaScript
// src/util/props.ts
function getRootName(object) {
if (object.type === "MemberExpression") {
return getRootName(object.object);
}
if (object.type === "CallExpression" || object.type === "NewExpression") {
return getRootName(object.callee);
}
if (object.type === "Identifier") {
return object.name;
}
if (object.type === "AwaitExpression") {
return getRootName(object.argument);
}
throw new Error("Root object is not an identifier");
}
// src/rules/safe-function-exports/errors.ts
var INVALID_FUNCTION_EXPORT = "Cannot have a firebase function that is not exported";
// src/rules/safe-function-exports/index.ts
var safeFunctionExports = {
create: (context) => {
const { importStarName = "functions" } = context.options[0] || {};
const hasFirebaseImports = context.getSourceCode().ast.body.some((node) => {
if (node.type === "ImportDeclaration") {
const namespaces = node.specifiers.filter((specifier) => specifier.type === "ImportNamespaceSpecifier");
if (namespaces.length === 0) {
return false;
}
return node.source.type === "Literal" && node.source.value === "firebase-functions" && namespaces.some((namespace) => namespace.local.name === importStarName);
}
return false;
});
if (!hasFirebaseImports) {
return {};
}
return {
VariableDeclaration(node) {
const isFirebaseFunction = node.kind === "const" && node.declarations.every((declaration) => {
if (declaration.init && declaration.init.type === "CallExpression" && declaration.init.callee.type === "MemberExpression" && declaration.init.callee.object) {
const rootName = getRootName(declaration.init.callee.object);
return rootName === importStarName;
}
return false;
});
if (!isFirebaseFunction) {
return;
}
const isExported = node.parent.type === "ExportNamedDeclaration";
if (!isExported) {
context.report({
node,
messageId: "INVALID_FUNCTION_EXPORT",
fix(fixer) {
return [fixer.insertTextBefore(node, "export ")];
}
});
}
}
};
},
meta: {
fixable: "code",
messages: { INVALID_FUNCTION_EXPORT },
docs: {
description: "Ensures that all firebase functions are exported in a file",
recommended: true,
category: "Possible Problems"
},
type: "problem"
}
};
// src/index.ts
var config = {
rules: {
"safe-function-exports": safeFunctionExports
}
};
module.exports = config;