eslint-plugin-formatjs 2.20.5 → 3.0.1

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.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  declare const plugin: {
2
2
  rules: {
3
- 'blacklist-elements': import("eslint").Rule.RuleModule;
3
+ 'blocklist-elements': import("eslint").Rule.RuleModule;
4
4
  'enforce-default-message': import("eslint").Rule.RuleModule;
5
5
  'enforce-description': import("eslint").Rule.RuleModule;
6
6
  'enforce-id': import("eslint").Rule.RuleModule;
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var tslib_1 = require("tslib");
4
- var blacklist_elements_1 = (0, tslib_1.__importDefault)(require("./rules/blacklist-elements"));
4
+ var blocklist_elements_1 = (0, tslib_1.__importDefault)(require("./rules/blocklist-elements"));
5
5
  var enforce_default_message_1 = (0, tslib_1.__importDefault)(require("./rules/enforce-default-message"));
6
6
  var enforce_description_1 = (0, tslib_1.__importDefault)(require("./rules/enforce-description"));
7
7
  var enforce_id_1 = (0, tslib_1.__importDefault)(require("./rules/enforce-id"));
@@ -16,7 +16,7 @@ var no_multiple_whitespaces_1 = (0, tslib_1.__importDefault)(require("./rules/no
16
16
  var no_offset_1 = (0, tslib_1.__importDefault)(require("./rules/no-offset"));
17
17
  var plugin = {
18
18
  rules: {
19
- 'blacklist-elements': blacklist_elements_1.default,
19
+ 'blocklist-elements': blocklist_elements_1.default,
20
20
  'enforce-default-message': enforce_default_message_1.default,
21
21
  'enforce-description': enforce_description_1.default,
22
22
  'enforce-id': enforce_id_1.default,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-formatjs",
3
- "version": "2.20.5",
3
+ "version": "3.0.1",
4
4
  "description": "ESLint plugin for formatjs",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -20,8 +20,8 @@
20
20
  },
21
21
  "homepage": "https://github.com/formatjs/formatjs#readme",
22
22
  "dependencies": {
23
- "@formatjs/icu-messageformat-parser": "2.0.17",
24
- "@formatjs/ts-transformer": "3.9.1",
23
+ "@formatjs/icu-messageformat-parser": "2.0.19",
24
+ "@formatjs/ts-transformer": "3.9.3",
25
25
  "@types/eslint": "8",
26
26
  "@typescript-eslint/typescript-estree": "^5.9.1",
27
27
  "emoji-regex": "^10.0.0",
@@ -1,4 +1,4 @@
1
1
  import { Rule } from 'eslint';
2
2
  declare const rule: Rule.RuleModule;
3
3
  export default rule;
4
- //# sourceMappingURL=blacklist-elements.d.ts.map
4
+ //# sourceMappingURL=blocklist-elements.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"blocklist-elements.d.ts","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-formatjs/rules/blocklist-elements.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,QAAQ,CAAA;AAkH3B,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UAyChB,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -7,7 +7,7 @@ var BlacklistElement = /** @class */ (function (_super) {
7
7
  (0, tslib_1.__extends)(BlacklistElement, _super);
8
8
  function BlacklistElement(type) {
9
9
  var _this = _super.call(this) || this;
10
- _this.message = "".concat(type, " element is blacklisted");
10
+ _this.message = "".concat(type, " element is blocklisted");
11
11
  return _this;
12
12
  }
13
13
  return BlacklistElement;
@@ -24,36 +24,36 @@ var Element;
24
24
  Element["plural"] = "plural";
25
25
  Element["tag"] = "tag";
26
26
  })(Element || (Element = {}));
27
- function verifyAst(blacklist, ast) {
27
+ function verifyAst(blocklist, ast) {
28
28
  for (var _i = 0, ast_1 = ast; _i < ast_1.length; _i++) {
29
29
  var el = ast_1[_i];
30
- if ((0, icu_messageformat_parser_1.isLiteralElement)(el) && blacklist.includes(Element.literal)) {
30
+ if ((0, icu_messageformat_parser_1.isLiteralElement)(el) && blocklist.includes(Element.literal)) {
31
31
  throw new BlacklistElement(Element.literal);
32
32
  }
33
- if ((0, icu_messageformat_parser_1.isArgumentElement)(el) && blacklist.includes(Element.argument)) {
33
+ if ((0, icu_messageformat_parser_1.isArgumentElement)(el) && blocklist.includes(Element.argument)) {
34
34
  throw new BlacklistElement(Element.argument);
35
35
  }
36
- if ((0, icu_messageformat_parser_1.isNumberElement)(el) && blacklist.includes(Element.number)) {
36
+ if ((0, icu_messageformat_parser_1.isNumberElement)(el) && blocklist.includes(Element.number)) {
37
37
  throw new BlacklistElement(Element.number);
38
38
  }
39
- if ((0, icu_messageformat_parser_1.isDateElement)(el) && blacklist.includes(Element.date)) {
39
+ if ((0, icu_messageformat_parser_1.isDateElement)(el) && blocklist.includes(Element.date)) {
40
40
  throw new BlacklistElement(Element.date);
41
41
  }
42
- if ((0, icu_messageformat_parser_1.isTimeElement)(el) && blacklist.includes(Element.time)) {
42
+ if ((0, icu_messageformat_parser_1.isTimeElement)(el) && blocklist.includes(Element.time)) {
43
43
  throw new BlacklistElement(Element.time);
44
44
  }
45
- if ((0, icu_messageformat_parser_1.isSelectElement)(el) && blacklist.includes(Element.select)) {
45
+ if ((0, icu_messageformat_parser_1.isSelectElement)(el) && blocklist.includes(Element.select)) {
46
46
  throw new BlacklistElement(Element.select);
47
47
  }
48
- if ((0, icu_messageformat_parser_1.isTagElement)(el) && blacklist.includes(Element.tag)) {
48
+ if ((0, icu_messageformat_parser_1.isTagElement)(el) && blocklist.includes(Element.tag)) {
49
49
  throw new BlacklistElement(Element.tag);
50
50
  }
51
51
  if ((0, icu_messageformat_parser_1.isPluralElement)(el)) {
52
- if (blacklist.includes(Element.plural)) {
52
+ if (blocklist.includes(Element.plural)) {
53
53
  throw new BlacklistElement(Element.argument);
54
54
  }
55
55
  if (el.pluralType === 'ordinal' &&
56
- blacklist.includes(Element.selectordinal)) {
56
+ blocklist.includes(Element.selectordinal)) {
57
57
  throw new BlacklistElement(Element.selectordinal);
58
58
  }
59
59
  }
@@ -61,7 +61,7 @@ function verifyAst(blacklist, ast) {
61
61
  var options = el.options;
62
62
  for (var _a = 0, _b = Object.keys(options); _a < _b.length; _a++) {
63
63
  var selector = _b[_a];
64
- verifyAst(blacklist, options[selector].value);
64
+ verifyAst(blocklist, options[selector].value);
65
65
  }
66
66
  }
67
67
  }
@@ -71,8 +71,8 @@ function checkNode(context, node) {
71
71
  if (!msgs.length) {
72
72
  return;
73
73
  }
74
- var blacklist = context.options[0];
75
- if (!Array.isArray(blacklist) || !blacklist.length) {
74
+ var blocklist = context.options[0];
75
+ if (!Array.isArray(blocklist) || !blocklist.length) {
76
76
  return;
77
77
  }
78
78
  for (var _i = 0, msgs_1 = msgs; _i < msgs_1.length; _i++) {
@@ -100,7 +100,7 @@ var rule = {
100
100
  description: 'Disallow specific elements in ICU message format',
101
101
  category: 'Errors',
102
102
  recommended: false,
103
- url: 'https://formatjs.io/docs/tooling/linter#blacklist-elements',
103
+ url: 'https://formatjs.io/docs/tooling/linter#blocklist-elements',
104
104
  },
105
105
  fixable: 'code',
106
106
  schema: [
@@ -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 allowlisted 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 allowlist 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
  },
@@ -1 +0,0 @@
1
- {"version":3,"file":"blacklist-elements.d.ts","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-formatjs/rules/blacklist-elements.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,QAAQ,CAAA;AAkH3B,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UAyChB,CAAA;AAED,eAAe,IAAI,CAAA"}