UNPKG

changelog-guru

Version:
106 lines (105 loc) 5.08 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _MarkRule_markers, _MarkRule_sections; import { findSame } from '../../utils/text.js'; import { CommitChangeType } from '../entities/Commit.js'; import { SectionOrder, SectionPosition } from '../entities/Section.js'; import { BaseRule } from './BaseRule.js'; export var MarkerType; (function (MarkerType) { MarkerType["Breaking"] = "break"; MarkerType["Deprecated"] = "deprecated"; MarkerType["Grouped"] = "group"; MarkerType["Ignore"] = "ignore"; MarkerType["Important"] = "important"; })(MarkerType || (MarkerType = {})); const POSITIONS_MAP = { [MarkerType.Breaking]: SectionPosition.Header, [MarkerType.Deprecated]: SectionPosition.Header, [MarkerType.Important]: SectionPosition.Body, }; const COMMIT_CHANGE_TYPES_MAP = { [MarkerType.Breaking]: CommitChangeType.BreakingChanges, [MarkerType.Deprecated]: CommitChangeType.Deprecated, [MarkerType.Important]: CommitChangeType.Important, }; class MarkRule extends BaseRule { constructor(config) { super(config); _MarkRule_markers.set(this, new Set()); _MarkRule_sections.set(this, new Map()); __classPrivateFieldSet(this, _MarkRule_markers, new Set(Object.values(MarkerType)), "f"); } lint({ task, body }) { const [markersLine, blackLine, bodyFirstLine] = body; const markers = this.getMarkersFrom(markersLine); const names = Object.values(MarkerType); if (markers.length) { task.log(`Markers: ${markersLine}`); markers.forEach(([marker, value, type]) => { if (!names.some(name => name === marker)) task.error(`Unexpected marker {bold !${type}}`); if (marker === MarkerType.Grouped && !value) task.error('{bold !group} name is empty'); }); if (bodyFirstLine && blackLine && blackLine.trim().length) { task.error('Missing blank line between markers and body'); } } else if (markersLine) { task.error('Missing blank line between header and body'); } } parse({ commit, context }) { const markers = this.getMarkersFrom(commit.body[0]); let section; commit.isIgnored = !!markers.find(([marker]) => marker === MarkerType.Ignore); if (!commit.isIgnored) { markers.forEach(([marker, name]) => { if (COMMIT_CHANGE_TYPES_MAP[marker]) commit.changeType = COMMIT_CHANGE_TYPES_MAP[marker]; if (marker === MarkerType.Grouped && name && context) section = context.addSection({ name }); if ((section = section || __classPrivateFieldGet(this, _MarkRule_sections, "f").get(marker))) section.add(commit); }); } } prepare({ context }) { const order = SectionOrder.Min; __classPrivateFieldSet(this, _MarkRule_sections, new Map(Object.entries(this.config.joins).reduce((acc, [type, name]) => { const section = context.addSection({ name, position: POSITIONS_MAP[type] ?? SectionPosition.None, order }); if (section) { acc.push([type, section]); } return acc; }, [])), "f"); } getMarkersFrom(text) { const markers = []; if (text) { const expression = /!(?<name>[a-z]+)(\((?<value>[\w &]+)\)|)( |)/gi; let match; while ((match = expression.exec(text)) !== null) { const { name, value } = match.groups ?? {}; if (name) { const marker = findSame(name, [...__classPrivateFieldGet(this, _MarkRule_markers, "f")]); if (marker) markers.push([marker, value ?? '', name]); } } } return markers; } } _MarkRule_markers = new WeakMap(), _MarkRule_sections = new WeakMap(); export default MarkRule;