UNPKG

eslint-plugin-ng-module-sort

Version:

Sort Angular and NestJS module imports, declarations, exports, controls, etc.

75 lines 2.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decoratorArrayItemsRule = exports.DECORATOR_ARRAY_ITEMS_NAME = void 0; const order_check_util_1 = require("./order-check.util"); const order_fixer_util_1 = require("./order-fixer.util"); const utils_1 = require("../../utils"); const defaultOptions = [ { reverseSort: false, extraDecorators: [], extraProperties: [], }, ]; const meta = { type: 'layout', docs: { description: 'Checks if the Angular or NestJS module related metadata has ordered arrays, and provide an autofix to sort them.', }, messages: { wrongOrderOfDecoratorArrayItems: 'Run `eslint --fix .` to sort the members of {{ property }}.', }, fixable: 'code', defaultOptions, schema: [ { type: 'object', properties: { reverseSort: { type: 'boolean' }, extraDecorators: { type: 'array', items: { type: 'string' } }, extraProperties: { type: 'array', items: { type: 'string' } }, }, additionalProperties: false, }, ], }; function create(context) { const { reverseSort, extraDecorators, extraProperties } = { ...defaultOptions[0], ...context.options[0], }; return { Decorator(node) { const properties = (0, utils_1.getPropertiesOfDecorator)(node, extraDecorators); if (!properties) return; const knownProperties = (0, utils_1.getKnownProperties)(properties, extraProperties); if (!knownProperties || knownProperties.length === 0) return; for (const prop of knownProperties) { const keyName = prop.key?.name; const arrayExpression = prop.value; const elements = arrayExpression.elements; if (!elements || !elements.length) return; const isOrdered = (0, order_check_util_1.orderCheck)(context, elements, reverseSort); if (isOrdered) return; context.report({ node: arrayExpression, messageId: 'wrongOrderOfDecoratorArrayItems', data: { property: keyName }, fix: (fixer) => (0, order_fixer_util_1.orderFixer)(fixer, context, arrayExpression, reverseSort), }); } }, }; } exports.DECORATOR_ARRAY_ITEMS_NAME = 'decorator-array-items'; exports.decoratorArrayItemsRule = (0, utils_1.ruleCreator)({ name: exports.DECORATOR_ARRAY_ITEMS_NAME, meta, defaultOptions, create, }); //# sourceMappingURL=index.js.map