eslint-plugin-formatjs 4.0.2 → 4.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +0 -0
- package/README.md +0 -0
- package/index.d.ts +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +18 -16
- package/package.json +4 -4
- package/rules/blocklist-elements.d.ts +0 -0
- package/rules/blocklist-elements.d.ts.map +1 -1
- package/rules/blocklist-elements.js +19 -27
- package/rules/enforce-default-message.d.ts +0 -0
- package/rules/enforce-default-message.d.ts.map +1 -1
- package/rules/enforce-default-message.js +11 -12
- package/rules/enforce-description.d.ts +0 -0
- package/rules/enforce-description.d.ts.map +1 -1
- package/rules/enforce-description.js +7 -10
- package/rules/enforce-id.d.ts +0 -0
- package/rules/enforce-id.d.ts.map +1 -1
- package/rules/enforce-id.js +35 -44
- package/rules/enforce-placeholders.d.ts +0 -0
- package/rules/enforce-placeholders.d.ts.map +1 -1
- package/rules/enforce-placeholders.js +26 -31
- package/rules/enforce-plural-rules.d.ts +0 -0
- package/rules/enforce-plural-rules.d.ts.map +1 -1
- package/rules/enforce-plural-rules.js +26 -35
- package/rules/no-camel-case.d.ts +0 -0
- package/rules/no-camel-case.d.ts.map +1 -1
- package/rules/no-camel-case.js +19 -27
- package/rules/no-complex-selectors.d.ts +0 -0
- package/rules/no-complex-selectors.d.ts.map +1 -1
- package/rules/no-complex-selectors.js +32 -24
- package/rules/no-emoji.d.ts +0 -0
- package/rules/no-emoji.d.ts.map +1 -1
- package/rules/no-emoji.js +10 -13
- package/rules/no-id.d.ts +0 -0
- package/rules/no-id.d.ts.map +1 -1
- package/rules/no-id.js +11 -17
- package/rules/no-invalid-icu.d.ts +4 -0
- package/rules/no-invalid-icu.d.ts.map +1 -0
- package/rules/no-invalid-icu.js +54 -0
- package/rules/no-literal-string-in-jsx.d.ts +0 -0
- package/rules/no-literal-string-in-jsx.d.ts.map +1 -1
- package/rules/no-literal-string-in-jsx.js +41 -35
- package/rules/no-multiple-plurals.d.ts +0 -0
- package/rules/no-multiple-plurals.d.ts.map +1 -1
- package/rules/no-multiple-plurals.js +19 -28
- package/rules/no-multiple-whitespaces.d.ts +0 -0
- package/rules/no-multiple-whitespaces.d.ts.map +1 -1
- package/rules/no-multiple-whitespaces.js +24 -37
- package/rules/no-offset.d.ts +0 -0
- package/rules/no-offset.d.ts.map +1 -1
- package/rules/no-offset.js +18 -26
- package/util.d.ts +2 -0
- package/util.d.ts.map +1 -1
- package/util.js +45 -43
package/util.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractMessages = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
exports.extractMessages = exports.getSettings = void 0;
|
|
4
|
+
const FORMAT_FUNCTION_NAMES = new Set(['$formatMessage', 'formatMessage']);
|
|
5
|
+
const COMPONENT_NAMES = new Set(['FormattedMessage']);
|
|
6
|
+
const DECLARATION_FUNCTION_NAMES = new Set(['defineMessage']);
|
|
7
|
+
function getSettings({ settings }) {
|
|
8
|
+
return settings.formatjs ?? settings;
|
|
9
|
+
}
|
|
10
|
+
exports.getSettings = getSettings;
|
|
8
11
|
function isStringLiteral(node) {
|
|
9
12
|
return node.type === 'Literal' && typeof node.value === 'string';
|
|
10
13
|
}
|
|
@@ -19,7 +22,7 @@ function staticallyEvaluateStringConcat(node) {
|
|
|
19
22
|
return [String(node.left.value) + node.right.value, true];
|
|
20
23
|
}
|
|
21
24
|
if (node.left.type === 'BinaryExpression') {
|
|
22
|
-
|
|
25
|
+
const [result, isStaticallyEvaluatable] = staticallyEvaluateStringConcat(node.left);
|
|
23
26
|
return [result + node.right.value, isStaticallyEvaluatable];
|
|
24
27
|
}
|
|
25
28
|
return ['', false];
|
|
@@ -48,20 +51,19 @@ function extractMessageDescriptor(node) {
|
|
|
48
51
|
if (!node || node.type !== 'ObjectExpression') {
|
|
49
52
|
return;
|
|
50
53
|
}
|
|
51
|
-
|
|
54
|
+
const result = {
|
|
52
55
|
message: {},
|
|
53
56
|
messageNode: undefined,
|
|
54
57
|
messagePropNode: undefined,
|
|
55
58
|
descriptionNode: undefined,
|
|
56
59
|
idValueNode: undefined,
|
|
57
60
|
};
|
|
58
|
-
for (
|
|
59
|
-
var prop = _a[_i];
|
|
61
|
+
for (const prop of node.properties) {
|
|
60
62
|
if (prop.type !== 'Property' || prop.key.type !== 'Identifier') {
|
|
61
63
|
continue;
|
|
62
64
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
const valueNode = prop.value;
|
|
66
|
+
let value = undefined;
|
|
65
67
|
if (isStringLiteral(valueNode)) {
|
|
66
68
|
value = valueNode.value;
|
|
67
69
|
}
|
|
@@ -69,9 +71,9 @@ function extractMessageDescriptor(node) {
|
|
|
69
71
|
value = valueNode.quasis[0].value.cooked;
|
|
70
72
|
}
|
|
71
73
|
else if (valueNode.type === 'BinaryExpression') {
|
|
72
|
-
|
|
74
|
+
const [result, isStatic] = staticallyEvaluateStringConcat(valueNode);
|
|
73
75
|
if (isStatic) {
|
|
74
|
-
value =
|
|
76
|
+
value = result;
|
|
75
77
|
}
|
|
76
78
|
}
|
|
77
79
|
switch (prop.key.name) {
|
|
@@ -97,8 +99,8 @@ function extractMessageDescriptorFromJSXElement(node) {
|
|
|
97
99
|
if (!node || !node.attributes) {
|
|
98
100
|
return;
|
|
99
101
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
let values;
|
|
103
|
+
const result = {
|
|
102
104
|
message: {},
|
|
103
105
|
messageNode: undefined,
|
|
104
106
|
messagePropNode: undefined,
|
|
@@ -106,9 +108,8 @@ function extractMessageDescriptorFromJSXElement(node) {
|
|
|
106
108
|
idValueNode: undefined,
|
|
107
109
|
idPropNode: undefined,
|
|
108
110
|
};
|
|
109
|
-
|
|
110
|
-
for (
|
|
111
|
-
var prop = _a[_i];
|
|
111
|
+
let hasSpreadAttribute = false;
|
|
112
|
+
for (const prop of node.attributes) {
|
|
112
113
|
// We can't analyze spread attr
|
|
113
114
|
if (prop.type === 'JSXSpreadAttribute') {
|
|
114
115
|
hasSpreadAttribute = true;
|
|
@@ -116,19 +117,19 @@ function extractMessageDescriptorFromJSXElement(node) {
|
|
|
116
117
|
if (prop.type !== 'JSXAttribute' || prop.name.type !== 'JSXIdentifier') {
|
|
117
118
|
continue;
|
|
118
119
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
120
|
+
const key = prop.name;
|
|
121
|
+
let valueNode = prop.value;
|
|
122
|
+
let value = undefined;
|
|
122
123
|
if (valueNode) {
|
|
123
124
|
if (isStringLiteral(valueNode)) {
|
|
124
125
|
value = valueNode.value;
|
|
125
126
|
}
|
|
126
|
-
else if (
|
|
127
|
-
|
|
127
|
+
else if (valueNode?.type === 'JSXExpressionContainer') {
|
|
128
|
+
const { expression } = valueNode;
|
|
128
129
|
if (expression.type === 'BinaryExpression') {
|
|
129
|
-
|
|
130
|
+
const [result, isStatic] = staticallyEvaluateStringConcat(expression);
|
|
130
131
|
if (isStatic) {
|
|
131
|
-
value =
|
|
132
|
+
value = result;
|
|
132
133
|
}
|
|
133
134
|
}
|
|
134
135
|
else if (isTemplateLiteralWithoutVar(expression)) {
|
|
@@ -158,7 +159,7 @@ function extractMessageDescriptorFromJSXElement(node) {
|
|
|
158
159
|
}
|
|
159
160
|
break;
|
|
160
161
|
case 'values':
|
|
161
|
-
if (
|
|
162
|
+
if (valueNode?.type === 'JSXExpressionContainer' &&
|
|
162
163
|
valueNode.expression.type === 'ObjectExpression') {
|
|
163
164
|
values = valueNode.expression;
|
|
164
165
|
}
|
|
@@ -177,35 +178,36 @@ function extractMessageDescriptors(node) {
|
|
|
177
178
|
if (!node || node.type !== 'ObjectExpression' || !node.properties.length) {
|
|
178
179
|
return [];
|
|
179
180
|
}
|
|
180
|
-
|
|
181
|
-
for (
|
|
182
|
-
var prop = _a[_i];
|
|
181
|
+
const msgs = [];
|
|
182
|
+
for (const prop of node.properties) {
|
|
183
183
|
if (prop.type !== 'Property') {
|
|
184
184
|
continue;
|
|
185
185
|
}
|
|
186
|
-
|
|
186
|
+
const msg = prop.value;
|
|
187
187
|
if (msg.type !== 'ObjectExpression') {
|
|
188
188
|
continue;
|
|
189
189
|
}
|
|
190
|
-
|
|
190
|
+
const nodeInfo = extractMessageDescriptor(msg);
|
|
191
191
|
if (nodeInfo) {
|
|
192
192
|
msgs.push(nodeInfo);
|
|
193
193
|
}
|
|
194
194
|
}
|
|
195
195
|
return msgs;
|
|
196
196
|
}
|
|
197
|
-
function extractMessages(node,
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
197
|
+
function extractMessages(node, { additionalComponentNames, additionalFunctionNames, excludeMessageDeclCalls, } = {}) {
|
|
198
|
+
const allFormatFunctionNames = Array.isArray(additionalFunctionNames)
|
|
199
|
+
? new Set([
|
|
200
|
+
...Array.from(FORMAT_FUNCTION_NAMES),
|
|
201
|
+
...additionalFunctionNames,
|
|
202
|
+
])
|
|
201
203
|
: FORMAT_FUNCTION_NAMES;
|
|
202
|
-
|
|
203
|
-
? new Set(
|
|
204
|
+
const allComponentNames = Array.isArray(additionalComponentNames)
|
|
205
|
+
? new Set([...Array.from(COMPONENT_NAMES), ...additionalComponentNames])
|
|
204
206
|
: COMPONENT_NAMES;
|
|
205
207
|
if (node.type === 'CallExpression') {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
208
|
+
const expr = node;
|
|
209
|
+
const args0 = expr.arguments[0];
|
|
210
|
+
const args1 = expr.arguments[1];
|
|
209
211
|
// We can't really analyze spread element
|
|
210
212
|
if (!args0 || args0.type === 'SpreadElement') {
|
|
211
213
|
return [];
|
|
@@ -214,21 +216,21 @@ function extractMessages(node, _a) {
|
|
|
214
216
|
isSingleMessageDescriptorDeclaration(node, DECLARATION_FUNCTION_NAMES)) ||
|
|
215
217
|
isIntlFormatMessageCall(node) ||
|
|
216
218
|
isSingleMessageDescriptorDeclaration(node, allFormatFunctionNames)) {
|
|
217
|
-
|
|
219
|
+
const msgDescriptorNodeInfo = extractMessageDescriptor(args0);
|
|
218
220
|
if (msgDescriptorNodeInfo && (!args1 || args1.type !== 'SpreadElement')) {
|
|
219
221
|
return [[msgDescriptorNodeInfo, args1]];
|
|
220
222
|
}
|
|
221
223
|
}
|
|
222
224
|
else if (!excludeMessageDeclCalls &&
|
|
223
225
|
isMultipleMessageDescriptorDeclaration(node)) {
|
|
224
|
-
return extractMessageDescriptors(args0).map(
|
|
226
|
+
return extractMessageDescriptors(args0).map(msg => [msg, undefined]);
|
|
225
227
|
}
|
|
226
228
|
}
|
|
227
229
|
else if (node.type === 'JSXOpeningElement' &&
|
|
228
230
|
node.name &&
|
|
229
231
|
node.name.type === 'JSXIdentifier' &&
|
|
230
232
|
allComponentNames.has(node.name.name)) {
|
|
231
|
-
|
|
233
|
+
const msgDescriptorNodeInfo = extractMessageDescriptorFromJSXElement(node);
|
|
232
234
|
if (msgDescriptorNodeInfo) {
|
|
233
235
|
return [msgDescriptorNodeInfo];
|
|
234
236
|
}
|