Skip to content

Commit

Permalink
fix: ban top level t() calls aside from t`` calls
Browse files Browse the repository at this point in the history
  • Loading branch information
braineo committed Dec 14, 2023
1 parent 7c588c4 commit 3fc28bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/rules/t-call-in-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const rule: RuleModule<string, readonly unknown[]> = {
recommended: 'error' as RuleRecommendation,
},
messages: {
default: 't`` call should be inside function',
default: 't`` and t() call should be inside function',
},
schema: [
{
Expand Down Expand Up @@ -56,7 +56,19 @@ const rule: RuleModule<string, readonly unknown[]> = {
['ClassDeclaration TaggedTemplateExpression'](node: TSESTree.TaggedTemplateExpression) {
handler(node)
},

['CallExpression:exit'](node: TSESTree.CallExpression) {
const scope = context.getScope()
if (
scope.type === 'module' &&
node.callee.type === TSESTree.AST_NODE_TYPES.Identifier &&
node.callee.name === 't'
) {
context.report({
node,
messageId: 'default',
})
}
},
['TaggedTemplateExpression:exit'](node: TSESTree.TaggedTemplateExpression) {
if (visited.has(node)) return
if (!isTTaggedTemplateExpression(node)) {
Expand Down
8 changes: 7 additions & 1 deletion tests/src/rules/t-call-in-function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ ruleTester.run<string, readonly unknown[]>('t-call-in-function', rule, {
padding: ${tokens.spacing20};\
`',
},
{
code: 'const a = () => {t("Hello")}',
},
],

invalid: [{ code: 't`Hello`', errors }],
invalid: [
{ code: 't`Hello`', errors },
{ code: 't("Hello")', errors },
],
})

0 comments on commit 3fc28bc

Please sign in to comment.