eslint-plugin-formatjs 2.20.2 → 2.21.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-formatjs",
3
- "version": "2.20.2",
3
+ "version": "2.21.0",
4
4
  "description": "ESLint plugin for formatjs",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -20,11 +20,11 @@
20
20
  },
21
21
  "homepage": "https://github.com/formatjs/formatjs#readme",
22
22
  "dependencies": {
23
- "@formatjs/icu-messageformat-parser": "2.0.16",
24
- "@formatjs/ts-transformer": "3.8.2",
23
+ "@formatjs/icu-messageformat-parser": "2.0.18",
24
+ "@formatjs/ts-transformer": "3.9.2",
25
25
  "@types/eslint": "8",
26
- "@typescript-eslint/typescript-estree": "^4.11.0",
27
- "emoji-regex": "^9.2.0",
26
+ "@typescript-eslint/typescript-estree": "^5.9.1",
27
+ "emoji-regex": "^10.0.0",
28
28
  "tslib": "^2.1.0",
29
29
  "typescript": "^4.5"
30
30
  },
@@ -1 +1 @@
1
- {"version":3,"file":"enforce-id.d.ts","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-formatjs/rules/enforce-id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,QAAQ,CAAA;;AAoF3B,wBA0CoB"}
1
+ {"version":3,"file":"enforce-id.d.ts","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-formatjs/rules/enforce-id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,QAAQ,CAAA;;AA0G3B,wBAgEoB"}
@@ -2,11 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var util_1 = require("../util");
4
4
  var ts_transformer_1 = require("@formatjs/ts-transformer");
5
- function checkNode(context, node) {
5
+ function checkNode(context, node, _a) {
6
+ var idInterpolationPattern = _a.idInterpolationPattern, idWhitelistRegexps = _a.idWhitelistRegexps;
6
7
  var msgs = (0, util_1.extractMessages)(node, context.settings);
7
- var options = context.options;
8
- var _a = options[0], opt = _a === void 0 ? {} : _a;
9
- var idInterpolationPattern = opt.idInterpolationPattern;
10
8
  var _loop_1 = function (defaultMessage, description, id, idPropNode, descriptionNode, messagePropNode) {
11
9
  if (!idInterpolationPattern && !idPropNode) {
12
10
  context.report({
@@ -28,6 +26,11 @@ function checkNode(context, node) {
28
26
  });
29
27
  }
30
28
  else {
29
+ if (idWhitelistRegexps &&
30
+ id &&
31
+ idWhitelistRegexps.some(function (r) { return r.test(id); })) {
32
+ return { value: void 0 };
33
+ }
31
34
  var correctId_1 = (0, ts_transformer_1.interpolateName)({
32
35
  resourcePath: context.getFilename(),
33
36
  }, idInterpolationPattern, {
@@ -36,9 +39,15 @@ function checkNode(context, node) {
36
39
  : defaultMessage,
37
40
  });
38
41
  if (id !== correctId_1) {
42
+ var message = "\"id\" does not match with hash pattern ".concat(idInterpolationPattern);
43
+ if (idWhitelistRegexps) {
44
+ message += " or whitelisted patterns [\"".concat(idWhitelistRegexps
45
+ .map(function (r) { return r.toString(); })
46
+ .join('", "'), "\"]");
47
+ }
39
48
  context.report({
40
49
  node: node,
41
- message: "\"id\" does not match with hash pattern ".concat(idInterpolationPattern, ".\nExpected: ").concat(correctId_1, "\nActual: ").concat(id),
50
+ message: "".concat(message, ".\nExpected: ").concat(correctId_1, "\nActual: ").concat(id),
42
51
  fix: function (fixer) {
43
52
  if (idPropNode) {
44
53
  if (idPropNode.type === 'JSXAttribute') {
@@ -59,7 +68,9 @@ function checkNode(context, node) {
59
68
  };
60
69
  for (var _i = 0, msgs_1 = msgs; _i < msgs_1.length; _i++) {
61
70
  var _b = msgs_1[_i][0], _c = _b.message, defaultMessage = _c.defaultMessage, description = _c.description, id = _c.id, idPropNode = _b.idPropNode, descriptionNode = _b.descriptionNode, messagePropNode = _b.messagePropNode;
62
- _loop_1(defaultMessage, description, id, idPropNode, descriptionNode, messagePropNode);
71
+ var state_1 = _loop_1(defaultMessage, description, id, idPropNode, descriptionNode, messagePropNode);
72
+ if (typeof state_1 === "object")
73
+ return state_1.value;
63
74
  }
64
75
  }
65
76
  exports.default = {
@@ -78,6 +89,14 @@ exports.default = {
78
89
  properties: {
79
90
  idInterpolationPattern: {
80
91
  type: 'string',
92
+ description: 'Pattern to verify ID against. Recommended value: [sha512:contenthash:base64:6]',
93
+ },
94
+ idWhitelist: {
95
+ type: 'array',
96
+ description: "An array of strings with regular expressions. This array allows whitelist custom ids for messages. For example '`\\\\.`' allows any id which has dot; `'^payment_.*'` - allows any custom id which has prefix `payment_`. Be aware that any backslash \\ provided via string must be escaped with an additional backslash.",
97
+ items: {
98
+ type: 'string',
99
+ },
81
100
  },
82
101
  },
83
102
  required: ['idInterpolationPattern'],
@@ -86,8 +105,17 @@ exports.default = {
86
105
  ],
87
106
  },
88
107
  create: function (context) {
108
+ var _a;
109
+ var tmp = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a[0];
110
+ var opts = {
111
+ idInterpolationPattern: tmp === null || tmp === void 0 ? void 0 : tmp.idInterpolationPattern,
112
+ };
113
+ if (Array.isArray(tmp === null || tmp === void 0 ? void 0 : tmp.idWhitelist)) {
114
+ var idWhitelist = tmp.idWhitelist;
115
+ opts.idWhitelistRegexps = idWhitelist.map(function (str) { return new RegExp(str, 'i'); });
116
+ }
89
117
  var callExpressionVisitor = function (node) {
90
- return checkNode(context, node);
118
+ return checkNode(context, node, opts);
91
119
  };
92
120
  if (context.parserServices.defineTemplateBodyVisitor) {
93
121
  return context.parserServices.defineTemplateBodyVisitor({
@@ -97,7 +125,9 @@ exports.default = {
97
125
  });
98
126
  }
99
127
  return {
100
- JSXOpeningElement: function (node) { return checkNode(context, node); },
128
+ JSXOpeningElement: function (node) {
129
+ return checkNode(context, node, opts);
130
+ },
101
131
  CallExpression: callExpressionVisitor,
102
132
  };
103
133
  },