-
Notifications
You must be signed in to change notification settings - Fork 655
Expand file tree
/
Copy pathl10n-no-i18nString-calls-module-instantiation.ts
More file actions
61 lines (53 loc) · 1.8 KB
/
l10n-no-i18nString-calls-module-instantiation.ts
File metadata and controls
61 lines (53 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import type {TSESTree} from '@typescript-eslint/utils';
import {createRule} from './utils/ruleCreator.ts';
type CallExpression = TSESTree.CallExpression;
// One of these AST node types must be an ancestor of an i18nString call.
const REQUIRED_ANCESTOR = new Set([
'ArrowFunctionExpression',
'PropertyDefinition',
'FunctionDeclaration',
'FunctionExpression',
'MethodDefinition',
]);
function isI18nStringCall(callExpression: CallExpression): boolean {
return (callExpression.callee.type === 'Identifier' && callExpression.callee.name === 'i18nString');
}
export default createRule({
name: 'l10n-no-i18nString-calls-module-instantiation',
meta: {
type: 'problem',
docs: {
description:
'Calls to i18nString are illegal during module instantiation time because translated strings are not yet available.',
category: 'Possible Errors',
},
messages: {
disallowedCall: 'Calls to i18nString are disallowed at module instantiation time. Use i18nLazyString instead.',
},
schema: [], // no options
},
defaultOptions: [],
create: function(context) {
const sourceCode = context.sourceCode;
return {
CallExpression(callExpression) {
if (!isI18nStringCall(callExpression)) {
return;
}
const ancestorTypes = sourceCode.getAncestors(callExpression).map(node => node.type);
const hasRequiredAncestor = ancestorTypes.some(
ancestorType => REQUIRED_ANCESTOR.has(ancestorType),
);
if (!hasRequiredAncestor) {
context.report({
node: callExpression,
messageId: 'disallowedCall',
});
}
},
};
},
});