eslint-plugin-ng-module-sort
Version:
Sort Angular and NestJS module imports, declarations, exports, controls, etc.
67 lines • 2.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decoratorArrayItemsRule = 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 name = 'decorator-array-items';
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.',
recommended: 'error',
},
messages: {
wrongOrderOfDecoratorArrayItems: 'Run `eslint --fix .` to sort the members of {{ property }}.',
},
fixable: 'code',
schema: [defaultOptions],
};
function create(context) {
var _a, _b;
const options = (((_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a[0]) || defaultOptions);
const reverseSort = (_b = options.reverseSort) !== null && _b !== void 0 ? _b : defaultOptions.reverseSort;
const extraDecorators = options.extraDecorators || defaultOptions.extraDecorators;
const extraProperties = options.extraProperties || defaultOptions.extraProperties;
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;
knownProperties.forEach((prop) => {
var _a;
const keyName = (_a = prop.key) === null || _a === void 0 ? void 0 : _a.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.decoratorArrayItemsRule = (0, utils_1.ruleCreator)({
name,
meta,
create,
defaultOptions: [],
});
//# sourceMappingURL=index.js.map