@o3r/eslint-plugin
Version:
The module provides in-house eslint plugins to use in your own eslint configuration.
64 lines (63 loc) • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.name = void 0;
const utils_1 = require("../../utils");
const utils_2 = require("../utils");
/** Rule Name */
exports.name = 'template-async-number-limitation';
const defaultOptions = [{
maximumAsyncOnTag: 5
}];
exports.default = (0, utils_1.createRule)({
name: exports.name,
meta: {
type: 'problem',
hasSuggestions: true,
docs: {
description: 'Ensures that your template does not use too many Async pipes that can slow down your application.'
},
schema: [
{
type: 'object',
properties: {
maximumAsyncOnTag: {
type: 'integer',
description: 'Maximum number of async pipe on a single HTML element'
}
},
additionalProperties: false
}
],
messages: {
tooManyAsyncOnTag: 'The HTML element contains {{asyncNumber}} async pipes, the limit is {{maximumAsyncOnTag}}'
},
fixable: undefined
},
defaultOptions,
create: (context, [options]) => {
const parserServices = (0, utils_2.getTemplateParserServices)(context);
const asyncRegExp = /\| *async\b/g;
const rule = ({ attributes, inputs, sourceSpan }) => {
const values = [
...attributes.map(({ value }) => value),
...inputs.map((attr) => attr.value.toString())
];
const asyncNumber = values
.reduce((acc, value) => acc + (value.match(asyncRegExp)?.length ?? 0), 0);
if (asyncNumber > options.maximumAsyncOnTag) {
const loc = parserServices.convertNodeSourceSpanToLoc(sourceSpan);
context.report({
messageId: 'tooManyAsyncOnTag',
data: {
asyncNumber,
maximumAsyncOnTag: options.maximumAsyncOnTag
},
loc
});
}
};
return {
Element: rule
};
}
});