forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl10n-i18nString-call-only-with-uistrings.js
51 lines (46 loc) · 1.34 KB
/
l10n-i18nString-call-only-with-uistrings.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
// 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';
function isI18nStringOrLazyCall(callExpression) {
return callExpression.callee.type === 'Identifier' &&
['i18nString', 'i18nLazyString'].includes(callExpression.callee.name);
}
function isArgumentValid(argument) {
if (!argument) {
return false;
}
if (argument.type !== 'MemberExpression') {
return false;
}
return argument.object.type === 'Identifier' && argument.object.name === 'UIStrings';
}
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Calling i18nString/i18nLazyString without using a UIStrings object is illegal.',
category: 'Possible Errors',
},
schema: [] // no options
},
create: function(context) {
return {
CallExpression(callExpression) {
if (!isI18nStringOrLazyCall(callExpression)) {
return;
}
const argument = callExpression.arguments[0];
if (!isArgumentValid(argument)) {
context.report({
node: callExpression,
message: 'Calling i18nString/i18nLazyString without using a UIStrings object is illegal.',
});
}
}
};
}
};