UNPKG

@masknet/eslint-plugin

Version:
79 lines 2.81 kB
const BASE_URL = 'https://dimensiondev.github.io/eslint-plugin/src/rules/'; export function createRule({ create, defaultOptions, meta, name, }) { if (meta.docs && !meta.docs.url) { meta.docs.url = new URL(name, BASE_URL).href; } const rule = { create(context) { const optionsWithDefault = applyDefault(defaultOptions, context.options); return create(context, optionsWithDefault); }, defaultOptions, meta, name, }; return rule; } export function ensureParserWithTypeInformation(parserServices) { if (!parserServices?.program) { throw new Error('see https://typescript-eslint.io/docs/linting/type-linting'); } } // Derived from @typescript-eslint/utils to avoid runtime dependency // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/utils/src/eslint-utils/deepMerge.ts function deepMerge( // eslint-disable-next-line @typescript-eslint/no-explicit-any first = {}, // eslint-disable-next-line @typescript-eslint/no-explicit-any second = {}) { // get the unique set of keys across both objects const keys = new Set([...Object.keys(first), ...Object.keys(second)]); return Object.fromEntries([...keys].map((key) => { const firstHasKey = key in first; const secondHasKey = key in second; const firstValue = first[key]; const secondValue = second[key]; let value; if (firstHasKey && secondHasKey) { if (typeof firstValue === 'object' && !Array.isArray(firstValue) && typeof secondValue === 'object' && !Array.isArray(secondValue)) { // object type value = deepMerge(firstValue, secondValue); } else { // value type value = secondValue; } } else if (firstHasKey) { value = firstValue; } else { value = secondValue; } return [key, value]; })); } // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/utils/src/eslint-utils/applyDefault.ts function applyDefault(defaultOptions, userOptions) { const options = structuredClone(defaultOptions); if (userOptions == null) { return options; } ; options.forEach((opt, i) => { if (userOptions[i] !== undefined) { const userOpt = userOptions[i]; if (typeof userOpt === 'object' && !Array.isArray(userOpt) && typeof opt === 'object' && !Array.isArray(opt)) { options[i] = deepMerge(opt, userOpt); } else { options[i] = userOpt; } } }); return options; } //# sourceMappingURL=rule.js.map