eslint-plugin-formatjs 4.8.0 → 4.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-formatjs",
3
- "version": "4.8.0",
3
+ "version": "4.9.1",
4
4
  "description": "ESLint plugin for formatjs",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -1 +1 @@
1
- {"version":3,"file":"enforce-placeholders.d.ts","sourceRoot":"","sources":["enforce-placeholders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,QAAQ,CAAA;AAyH3B,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UA4ChB,CAAA;AAED,eAAe,IAAI,CAAA"}
1
+ {"version":3,"file":"enforce-placeholders.d.ts","sourceRoot":"","sources":["enforce-placeholders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,QAAQ,CAAA;AAoI3B,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UA4ChB,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -63,7 +63,17 @@ function checkNode(context, node) {
63
63
  }
64
64
  }
65
65
  }
66
- const ast = (0, icu_messageformat_parser_1.parse)(defaultMessage, { ignoreTag: settings.ignoreTag });
66
+ let ast;
67
+ try {
68
+ ast = (0, icu_messageformat_parser_1.parse)(defaultMessage, { ignoreTag: settings.ignoreTag });
69
+ }
70
+ catch (e) {
71
+ context.report({
72
+ node: messageNode,
73
+ message: e instanceof Error ? e.message : String(e),
74
+ });
75
+ continue;
76
+ }
67
77
  const placeholderNames = collectPlaceholderNames(ast);
68
78
  const missingPlaceholders = [];
69
79
  placeholderNames.forEach(name => {
@@ -1 +1 @@
1
- {"version":3,"file":"no-complex-selectors.d.ts","sourceRoot":"","sources":["no-complex-selectors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,QAAQ,CAAA;AAiF3B,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UAiDhB,CAAA;AAED,eAAe,IAAI,CAAA"}
1
+ {"version":3,"file":"no-complex-selectors.d.ts","sourceRoot":"","sources":["no-complex-selectors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,QAAQ,CAAA;AAgH3B,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UAiDhB,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -2,17 +2,45 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const util_1 = require("../util");
4
4
  const icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
5
- const manipulator_1 = require("@formatjs/icu-messageformat-parser/manipulator");
6
5
  function calculateComplexity(ast) {
7
- if (ast.length === 1) {
8
- const el = ast[0];
9
- if ((0, icu_messageformat_parser_1.isPluralElement)(el) || (0, icu_messageformat_parser_1.isSelectElement)(el)) {
10
- return Object.keys(el.options).reduce((complexity, k) => {
11
- return complexity + calculateComplexity(el.options[k].value);
12
- }, 0);
6
+ // Dynamic programming: define a complexity function f, where:
7
+ // f(plural | select) = sum(f(option) for each option) * f(next element),
8
+ // f(tag) = f(first element of children) * f(next element),
9
+ // f(other) = f(next element),
10
+ // f(out of bound) = 1.
11
+ const complexityByNode = new Map();
12
+ return _calculate(ast, 0);
13
+ function _calculate(ast, index) {
14
+ const element = ast[index];
15
+ if (!element) {
16
+ return 1;
13
17
  }
18
+ const cachedComplexity = complexityByNode.get(element);
19
+ if (cachedComplexity !== undefined) {
20
+ return cachedComplexity;
21
+ }
22
+ let complexity;
23
+ switch (element.type) {
24
+ case icu_messageformat_parser_1.TYPE.plural:
25
+ case icu_messageformat_parser_1.TYPE.select: {
26
+ let sumOfOptions = 0;
27
+ for (const { value } of Object.values(element.options)) {
28
+ sumOfOptions += _calculate(value, 0);
29
+ }
30
+ complexity = sumOfOptions * _calculate(ast, index + 1);
31
+ break;
32
+ }
33
+ case icu_messageformat_parser_1.TYPE.tag:
34
+ complexity =
35
+ _calculate(element.children, 0) * _calculate(ast, index + 1);
36
+ break;
37
+ default:
38
+ complexity = _calculate(ast, index + 1);
39
+ break;
40
+ }
41
+ complexityByNode.set(element, complexity);
42
+ return complexity;
14
43
  }
15
- return 1;
16
44
  }
17
45
  function checkNode(context, node) {
18
46
  const settings = (0, util_1.getSettings)(context);
@@ -43,8 +71,7 @@ function checkNode(context, node) {
43
71
  }
44
72
  let complexity = 0;
45
73
  try {
46
- const hoistedAst = (0, manipulator_1.hoistSelectors)(ast);
47
- complexity = calculateComplexity(hoistedAst);
74
+ complexity = calculateComplexity(ast);
48
75
  }
49
76
  catch (e) {
50
77
  context.report({