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/theme-token-usage.cjs

52 lines
1.4 KiB

// @ts-check
const { ESLintUtils, AST_NODE_TYPES } = require('@typescript-eslint/utils');
const createRule = ESLintUtils.RuleCreator((name) => `https://github.com/grafana/grafana#${name}`);
const themeTokenUsage = createRule({
create(context) {
return {
Identifier: function (node) {
if (node.name === 'theme') {
const ancestors = context.getAncestors().reverse();
const paths = [];
let lastAncestor = null;
for (const ancestor of ancestors) {
if (ancestor.type === AST_NODE_TYPES.MemberExpression && ancestor.property.type === AST_NODE_TYPES.Identifier) {
paths.push(ancestor.property.name)
lastAncestor = ancestor;
} else {
break;
}
}
if (paths.length > 0 && lastAncestor) {
paths.unshift('theme');
context.report({
node: lastAncestor,
messageId: "themeTokenUsed",
data: {
identifier: paths.join('.')
}
})
}
}
}
};
},
name: 'theme-token-usage',
meta: {
type: 'problem',
docs: {
description: 'Check for theme token usage',
recommended: false,
},
messages: {
themeTokenUsed: '{{ identifier }}',
},
schema: [],
},
defaultOptions: [],
});
module.exports = themeTokenUsage;