@o3r/eslint-plugin
Version:
The module provides in-house eslint plugins to use in your own eslint configuration.
47 lines (46 loc) • 2.48 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCommentString = exports.getNodeComment = exports.isExtendingConfiguration = exports.defaultSupportedInterfaceNames = exports.createRule = void 0;
const node_fs_1 = require("node:fs");
const path = require("node:path");
const utils_1 = require("@typescript-eslint/utils");
/** Current package version (format: <major>.<minor>)*/
const version = JSON.parse((0, node_fs_1.readFileSync)(path.resolve(__dirname, '..', '..', 'package.json'), { encoding: 'utf8' })).version?.split('.').slice(0, 2).join('.') || '0.0';
/** ESLint rule generator */
// eslint-disable-next-line new-cap -- naming convention imposed by typescript-eslint
exports.createRule = utils_1.ESLintUtils.RuleCreator((name) => {
return `https://github.com/AmadeusITGroup/otter/tree/release/${version === '0.0' ? 'main' : version}/docs/linter/eslint-plugin/rules/${name}.md`;
});
/** Default supported interface names */
exports.defaultSupportedInterfaceNames = ['Configuration', 'NestedConfiguration'];
/**
* Returns true if the node extends one of the `supportedInterfaceNames`
* @param interfaceDeclNode
* @param supportedInterfaceNames
*/
const isExtendingConfiguration = (interfaceDeclNode, supportedInterfaceNames = []) => {
const supportedInterfaceNamesSet = new Set(supportedInterfaceNames.length > 0 ? supportedInterfaceNames : exports.defaultSupportedInterfaceNames);
return interfaceDeclNode?.type === utils_1.TSESTree.AST_NODE_TYPES.TSInterfaceDeclaration
&& !!interfaceDeclNode.extends?.some((ext) => ext.type === utils_1.TSESTree.AST_NODE_TYPES.TSInterfaceHeritage
&& ext.expression.type === utils_1.TSESTree.AST_NODE_TYPES.Identifier
&& supportedInterfaceNamesSet.has(ext.expression.name));
};
exports.isExtendingConfiguration = isExtendingConfiguration;
/**
* Returns the comment above the node
* @param node
* @param sourceCode
*/
const getNodeComment = (node, sourceCode) => {
const [comment] = node.parent?.type === utils_1.TSESTree.AST_NODE_TYPES.ExportNamedDeclaration
? sourceCode.getCommentsBefore(node.parent)
: sourceCode.getCommentsBefore(node);
return comment;
};
exports.getNodeComment = getNodeComment;
/**
* Wraps `commentValue` into a comment
* @param commentValue
*/
const createCommentString = (commentValue) => `/*${commentValue.replace(/\*\//g, '*\\/')}*/`;
exports.createCommentString = createCommentString;
;