forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl10n-no-i18nString-calls-module-instantiation.js
52 lines (47 loc) · 1.6 KB
/
l10n-no-i18nString-calls-module-instantiation.js
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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
// One of these AST nodes must be an ancestor of an i18nString call.
const REQUIRED_ANCESTOR = new Set([
'ArrowFunctionExpression',
'PropertyDefinition',
'FunctionDeclaration',
'FunctionExpression',
'MethodDefinition',
]);
function isI18nStringCall(callExpression) {
return callExpression.callee.type === 'Identifier' && callExpression.callee.name === 'i18nString';
}
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'Calls to i18nString are illegal during module instantiation time. Translated strings are not yet available',
category: 'Possible Errors',
},
schema: [] // no options
},
create: function(context) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
return {
CallExpression(callExpression) {
if (!isI18nStringCall(callExpression)) {
return;
}
const ancestors = ((sourceCode.getAncestors ? sourceCode.getAncestors(callExpression) : context.getAncestors()) || []).map(node => node.type);
const hasRequiredAncestor = ancestors.some(ancestor => REQUIRED_ANCESTOR.has(ancestor));
if (!hasRequiredAncestor) {
context.report({
node: callExpression,
message: 'Calls to i18nString in are disallowed at module instantiation time. Use i18nLazyString instead.',
});
}
}
};
}
};