eslint-plugin-formatjs
Advanced tools
Comparing version 4.8.0 to 4.9.0
{ | ||
"name": "eslint-plugin-formatjs", | ||
"version": "4.8.0", | ||
"version": "4.9.0", | ||
"description": "ESLint plugin for formatjs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,13 +5,41 @@ "use strict"; | ||
const icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser"); | ||
const manipulator_1 = require("@formatjs/icu-messageformat-parser/manipulator"); | ||
function calculateComplexity(ast) { | ||
if (ast.length === 1) { | ||
const el = ast[0]; | ||
if ((0, icu_messageformat_parser_1.isPluralElement)(el) || (0, icu_messageformat_parser_1.isSelectElement)(el)) { | ||
return Object.keys(el.options).reduce((complexity, k) => { | ||
return complexity + calculateComplexity(el.options[k].value); | ||
}, 0); | ||
// Dynamic programming: define a complexity function f, where: | ||
// f(plural | select) = sum(f(option) for each option) * f(next element), | ||
// f(tag) = f(first element of children) * f(next element), | ||
// f(other) = f(next element), | ||
// f(out of bound) = 1. | ||
const complexityByNode = new Map(); | ||
return _calculate(ast, 0); | ||
function _calculate(ast, index) { | ||
const element = ast[index]; | ||
if (!element) { | ||
return 1; | ||
} | ||
const cachedComplexity = complexityByNode.get(element); | ||
if (cachedComplexity !== undefined) { | ||
return cachedComplexity; | ||
} | ||
let complexity; | ||
switch (element.type) { | ||
case icu_messageformat_parser_1.TYPE.plural: | ||
case icu_messageformat_parser_1.TYPE.select: { | ||
let sumOfOptions = 0; | ||
for (const { value } of Object.values(element.options)) { | ||
sumOfOptions += _calculate(value, 0); | ||
} | ||
complexity = sumOfOptions * _calculate(ast, index + 1); | ||
break; | ||
} | ||
case icu_messageformat_parser_1.TYPE.tag: | ||
complexity = | ||
_calculate(element.children, 0) * _calculate(ast, index + 1); | ||
break; | ||
default: | ||
complexity = _calculate(ast, index + 1); | ||
break; | ||
} | ||
complexityByNode.set(element, complexity); | ||
return complexity; | ||
} | ||
return 1; | ||
} | ||
@@ -47,4 +75,3 @@ function checkNode(context, node) { | ||
try { | ||
const hoistedAst = (0, manipulator_1.hoistSelectors)(ast); | ||
complexity = calculateComplexity(hoistedAst); | ||
complexity = calculateComplexity(ast); | ||
} | ||
@@ -51,0 +78,0 @@ catch (e) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
91257
2200