eslint-plugin-formatjs 6.5.0 → 6.6.0

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.
Files changed (3) hide show
  1. package/index.js +71 -15
  2. package/index.js.map +1 -1
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -234,23 +234,36 @@ let Element = /* @__PURE__ */ function(Element) {
234
234
  Element["tag"] = "tag";
235
235
  return Element;
236
236
  }({});
237
+ function isBlocklisted(blocklist, type, element) {
238
+ return blocklist.some((entry) => {
239
+ if (typeof entry === "string") return entry === type;
240
+ if (entry.type !== type) return false;
241
+ if (!("value" in element) || element.value !== entry.allow.variable) return true;
242
+ const allowedOptions = entry.allow.options;
243
+ if (!allowedOptions) return false;
244
+ if (!isSelectElement(element) && !isPluralElement(element)) return true;
245
+ const options = Object.keys(element.options);
246
+ return options.length !== allowedOptions.length || options.some((option) => !allowedOptions.includes(option));
247
+ });
248
+ }
237
249
  function verifyAst$7(blocklist, ast) {
238
250
  const errors = [];
239
251
  for (const el of ast) {
240
- if (isLiteralElement(el) && blocklist.includes("literal")) errors.push(getMessage("literal"));
241
- if (isArgumentElement(el) && blocklist.includes("argument")) errors.push(getMessage("argument"));
242
- if (isNumberElement(el) && blocklist.includes("number")) errors.push(getMessage("number"));
243
- if (isDateElement(el) && blocklist.includes("date")) errors.push(getMessage("date"));
244
- if (isTimeElement(el) && blocklist.includes("time")) errors.push(getMessage("time"));
245
- if (isSelectElement(el) && blocklist.includes("select")) errors.push(getMessage("select"));
246
- if (isTagElement(el) && blocklist.includes("tag")) errors.push(getMessage("tag"));
252
+ if (isLiteralElement(el) && isBlocklisted(blocklist, "literal", el)) errors.push(getMessage("literal"));
253
+ if (isArgumentElement(el) && isBlocklisted(blocklist, "argument", el)) errors.push(getMessage("argument"));
254
+ if (isNumberElement(el) && isBlocklisted(blocklist, "number", el)) errors.push(getMessage("number"));
255
+ if (isDateElement(el) && isBlocklisted(blocklist, "date", el)) errors.push(getMessage("date"));
256
+ if (isTimeElement(el) && isBlocklisted(blocklist, "time", el)) errors.push(getMessage("time"));
257
+ if (isSelectElement(el) && isBlocklisted(blocklist, "select", el)) errors.push(getMessage("select"));
258
+ if (isTagElement(el) && isBlocklisted(blocklist, "tag", el)) errors.push(getMessage("tag"));
259
+ if (isTagElement(el)) errors.push(...verifyAst$7(blocklist, el.children));
247
260
  if (isPluralElement(el)) {
248
- if (blocklist.includes("plural")) errors.push(getMessage("argument"));
249
- if (el.pluralType === "ordinal" && blocklist.includes("selectordinal")) errors.push(getMessage("selectordinal"));
261
+ if (isBlocklisted(blocklist, "plural", el)) errors.push(getMessage("argument"));
262
+ if (el.pluralType === "ordinal" && isBlocklisted(blocklist, "selectordinal", el)) errors.push(getMessage("selectordinal"));
250
263
  }
251
264
  if (isSelectElement(el) || isPluralElement(el)) {
252
265
  const { options } = el;
253
- for (const selector of Object.keys(options)) verifyAst$7(blocklist, options[selector].value);
266
+ for (const selector of Object.keys(options)) errors.push(...verifyAst$7(blocklist, options[selector].value));
254
267
  }
255
268
  }
256
269
  return errors;
@@ -291,10 +304,33 @@ const rule$20 = {
291
304
  fixable: "code",
292
305
  schema: [{
293
306
  type: "array",
294
- items: {
307
+ items: { oneOf: [{
295
308
  type: "string",
296
309
  enum: Object.keys(Element)
297
- }
310
+ }, {
311
+ type: "object",
312
+ additionalProperties: false,
313
+ required: ["type", "allow"],
314
+ properties: {
315
+ type: {
316
+ type: "string",
317
+ enum: Object.keys(Element)
318
+ },
319
+ allow: {
320
+ type: "object",
321
+ additionalProperties: false,
322
+ required: ["variable"],
323
+ properties: {
324
+ variable: { type: "string" },
325
+ options: {
326
+ type: "array",
327
+ uniqueItems: true,
328
+ items: { type: "string" }
329
+ }
330
+ }
331
+ }
332
+ }
333
+ }] }
298
334
  }],
299
335
  messages: {
300
336
  ...CORE_MESSAGES,
@@ -4537,7 +4573,7 @@ var package_exports = /* @__PURE__ */ __exportAll({
4537
4573
  version: () => version$1
4538
4574
  });
4539
4575
  var name$1 = "eslint-plugin-formatjs";
4540
- var version$1 = "6.5.0";
4576
+ var version$1 = "6.6.0";
4541
4577
  var description = "ESLint plugin for formatjs";
4542
4578
  var keywords = [
4543
4579
  "eslint",
@@ -4639,7 +4675,17 @@ plugin.configs = {
4639
4675
  other: true
4640
4676
  }],
4641
4677
  "formatjs/no-literal-string-in-jsx": ["error", { props: { include: [["*", "{label,placeholder,title}"]] } }],
4642
- "formatjs/blocklist-elements": ["error", ["selectordinal"]],
4678
+ "formatjs/blocklist-elements": ["error", ["selectordinal", {
4679
+ type: "select",
4680
+ allow: {
4681
+ variable: "gender",
4682
+ options: [
4683
+ "male",
4684
+ "female",
4685
+ "other"
4686
+ ]
4687
+ }
4688
+ }]],
4643
4689
  "formatjs/prefer-full-sentence": "error"
4644
4690
  }
4645
4691
  },
@@ -4663,7 +4709,17 @@ plugin.configs = {
4663
4709
  other: true
4664
4710
  }],
4665
4711
  "formatjs/no-literal-string-in-jsx": ["warn", { props: { include: [["*", "{label,placeholder,title}"]] } }],
4666
- "formatjs/blocklist-elements": ["error", ["selectordinal"]],
4712
+ "formatjs/blocklist-elements": ["error", ["selectordinal", {
4713
+ type: "select",
4714
+ allow: {
4715
+ variable: "gender",
4716
+ options: [
4717
+ "male",
4718
+ "female",
4719
+ "other"
4720
+ ]
4721
+ }
4722
+ }]],
4667
4723
  "formatjs/prefer-full-sentence": "error"
4668
4724
  }
4669
4725
  }