eslint-plugin-formatjs 6.4.6 → 6.4.8

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/index.js CHANGED
@@ -27,22 +27,54 @@ const DECLARATION_FUNCTION_NAMES = new Set(["defineMessage"]);
27
27
  function getSettings({ settings }) {
28
28
  return settings.formatjs ?? settings;
29
29
  }
30
- function isStringLiteral(node) {
31
- return node.type === "Literal" && typeof node.value === "string";
32
- }
33
30
  function isTemplateLiteralWithoutVar(node) {
34
31
  return node.type === "TemplateLiteral" && node.quasis.length === 1;
35
32
  }
36
- function staticallyEvaluateStringConcat(node) {
37
- const right = node.right;
38
- const left = node.left;
39
- if (!isStringLiteral(right)) return ["", false];
40
- if (isStringLiteral(left)) return [left.value + right.value, true];
41
- if (node.left.type === "BinaryExpression") {
42
- const [result, isStaticallyEvaluatable] = staticallyEvaluateStringConcat(node.left);
43
- return [result + right.value, isStaticallyEvaluatable];
33
+ function getStaticStringFromTemplateLiteral(node) {
34
+ return node.quasis.length === 1 ? node.quasis[0].value.cooked ?? void 0 : void 0;
35
+ }
36
+ function isStaticMessageExpression(node) {
37
+ switch (node.type) {
38
+ case "ArrayPattern":
39
+ case "AssignmentPattern":
40
+ case "ObjectPattern": return false;
41
+ default: return true;
42
+ }
43
+ }
44
+ function getStaticStringFromMessageExpression(node) {
45
+ switch (node.type) {
46
+ case "TSAsExpression":
47
+ case "TSSatisfiesExpression":
48
+ case "TSNonNullExpression":
49
+ case "TSTypeAssertion": return getStaticStringFromMessageExpression(node.expression);
50
+ case "Literal": return typeof node.value === "string" ? node.value : void 0;
51
+ case "TemplateLiteral": return getStaticStringFromTemplateLiteral(node);
52
+ case "TaggedTemplateExpression":
53
+ if (!isTemplateLiteralWithoutVar(node.quasi)) throw new Error("Tagged template expression must be no substitution");
54
+ return getStaticStringFromTemplateLiteral(node.quasi);
55
+ case "BinaryExpression": {
56
+ const [result, isStaticallyEvaluatable] = staticallyEvaluateStringConcat(node);
57
+ return isStaticallyEvaluatable ? result : void 0;
58
+ }
59
+ }
60
+ }
61
+ function getStaticStringFromBinaryExpressionOperand(node) {
62
+ switch (node.type) {
63
+ case "TSAsExpression":
64
+ case "TSSatisfiesExpression":
65
+ case "TSNonNullExpression":
66
+ case "TSTypeAssertion": return getStaticStringFromBinaryExpressionOperand(node.expression);
67
+ case "Literal": return typeof node.value === "string" ? node.value : void 0;
68
+ case "BinaryExpression": {
69
+ const [result, isStaticallyEvaluatable] = staticallyEvaluateStringConcat(node);
70
+ return isStaticallyEvaluatable ? result : void 0;
71
+ }
44
72
  }
45
- return ["", false];
73
+ }
74
+ function staticallyEvaluateStringConcat(node) {
75
+ const right = getStaticStringFromBinaryExpressionOperand(node.right);
76
+ const left = getStaticStringFromBinaryExpressionOperand(node.left);
77
+ return left !== void 0 && right !== void 0 ? [left + right, true] : ["", false];
46
78
  }
47
79
  function isIntlFormatMessageCall(node) {
48
80
  if (node.type !== "CallExpression") return false;
@@ -72,17 +104,7 @@ function extractMessageDescriptor(node) {
72
104
  const propName = prop.key.name;
73
105
  if (propName !== "id" && propName !== "defaultMessage" && propName !== "description") continue;
74
106
  const valueNode = prop.value;
75
- let value = void 0;
76
- if (isStringLiteral(valueNode)) value = valueNode.value;
77
- else if (isTemplateLiteralWithoutVar(valueNode)) value = valueNode.quasis[0].value.cooked ?? void 0;
78
- else if (valueNode.type === "TaggedTemplateExpression") {
79
- const { quasi } = valueNode;
80
- if (!isTemplateLiteralWithoutVar(quasi)) throw new Error("Tagged template expression must be no substitution");
81
- value = quasi.quasis[0].value.cooked ?? void 0;
82
- } else if (valueNode.type === "BinaryExpression") {
83
- const [result, isStatic] = staticallyEvaluateStringConcat(valueNode);
84
- if (isStatic) value = result;
85
- }
107
+ const value = isStaticMessageExpression(valueNode) ? getStaticStringFromMessageExpression(valueNode) : void 0;
86
108
  switch (propName) {
87
109
  case "defaultMessage":
88
110
  result.messagePropNode = prop;
@@ -123,18 +145,10 @@ function extractMessageDescriptorFromJSXElement(node) {
123
145
  let valueNode = prop.value;
124
146
  let value = void 0;
125
147
  if (valueNode && isMessageProp) {
126
- if (isStringLiteral(valueNode)) value = valueNode.value;
148
+ if (valueNode.type === "Literal" && typeof valueNode.value === "string") value = valueNode.value;
127
149
  else if (valueNode?.type === "JSXExpressionContainer") {
128
150
  const { expression } = valueNode;
129
- if (expression.type === "BinaryExpression") {
130
- const [result, isStatic] = staticallyEvaluateStringConcat(expression);
131
- if (isStatic) value = result;
132
- } else if (isTemplateLiteralWithoutVar(expression)) value = expression.quasis[0].value.cooked ?? void 0;
133
- else if (expression.type === "TaggedTemplateExpression") {
134
- const { quasi } = expression;
135
- if (!isTemplateLiteralWithoutVar(quasi)) throw new Error("Tagged template expression must be no substitution");
136
- value = quasi.quasis[0].value.cooked ?? void 0;
137
- }
151
+ if (expression.type !== "JSXEmptyExpression") value = getStaticStringFromMessageExpression(expression);
138
152
  }
139
153
  }
140
154
  switch (keyName) {
@@ -4525,7 +4539,7 @@ var package_exports = /* @__PURE__ */ __exportAll({
4525
4539
  });
4526
4540
  var name$1 = "eslint-plugin-formatjs";
4527
4541
  var description = "ESLint plugin for formatjs";
4528
- var version$1 = "6.4.6";
4542
+ var version$1 = "6.4.8";
4529
4543
  var license = "MIT";
4530
4544
  var author = "Long Ho <holevietlong@gmail.com>";
4531
4545
  var type = "module";