The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/packages/grafana-eslint-rules/rules/no-translation-top-level.cjs

59 lines
1.6 KiB

// @ts-check
const { ESLintUtils, AST_NODE_TYPES } = require('@typescript-eslint/utils');
/**
* @typedef {import('@typescript-eslint/utils').TSESTree.Node} Node
* @typedef {import('@typescript-eslint/utils').TSESLint.RuleContext<string, unknown[]>} RuleContext
*/
const createRule = ESLintUtils.RuleCreator(
(name) => `https://github.com/grafana/grafana/blob/main/packages/grafana-eslint-rules/README.md#${name}`
);
/**
* @param {RuleContext} context
* @param {Node} node
* @returns {boolean}
*/
const isInFunction = (context, node) => {
const ancestors = context.sourceCode.getAncestors(node);
return ancestors.some((anc) => {
return [
AST_NODE_TYPES.ArrowFunctionExpression,
AST_NODE_TYPES.FunctionDeclaration,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.ClassDeclaration,
].includes(anc.type);
});
};
const noTranslationTopLevel = createRule({
create(context) {
return {
CallExpression(node) {
if (node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === 't') {
if (!isInFunction(context, node)) {
context.report({
node,
messageId: 'noMethodOutsideComponent',
});
}
}
},
};
},
name: 'no-translation-top-level',
meta: {
type: 'suggestion',
docs: {
description: 'Do not use translation functions outside of components',
},
messages: {
noMethodOutsideComponent: 'Do not use the t() function outside of a component or function',
},
schema: [],
},
defaultOptions: [],
});
module.exports = noTranslationTopLevel;