mirror of https://github.com/grafana/grafana
Chore: Emit theme token usage metrics (#72500)
* create rule to find instances of theme variables * emit theme token usage metrics * move awking into script * make sure theme usage is covering ts and tsx files, remove commented linespull/72527/head
parent
50bd27b665
commit
ae4810f854
@ -1,7 +1,9 @@ |
||||
const noAriaLabelSelectors = require('./rules/no-aria-label-e2e-selectors.cjs'); |
||||
const themeTokenUsage = require('./rules/theme-token-usage.cjs'); |
||||
|
||||
module.exports = { |
||||
rules: { |
||||
'no-aria-label-selectors': noAriaLabelSelectors, |
||||
'theme-token-usage': themeTokenUsage, |
||||
}, |
||||
}; |
||||
|
@ -0,0 +1,51 @@ |
||||
// @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; |
Loading…
Reference in new issue