Skip to content

Commit 805d93d

Browse files
committed
chore: tidy the code
1 parent 1254504 commit 805d93d

File tree

3 files changed

+100
-94
lines changed

3 files changed

+100
-94
lines changed

src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ async function main() {
2727
const rawCommits = await getGitDiff(config.from, config.to);
2828

2929
// Parse commits as conventional commits
30-
const commits = parseCommits(rawCommits, config)
31-
const filteredCommits = filterCommits(commits, config)
30+
const commits = parseCommits(rawCommits, config);
31+
const filteredCommits = filterCommits(commits, config);
3232

3333
// Bump version optionally
3434
if (args.bump || args.release) {

src/git.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export interface GitCommit extends RawGitCommit {
2828
}
2929

3030
export interface RevertPair {
31-
shortRevertingHash: string
32-
revertedHash: string
31+
shortRevertingHash: string;
32+
revertedHash: string;
3333
}
3434

3535
export async function getLastGitTag() {
@@ -99,7 +99,7 @@ const ConventionalCommitRegex =
9999
const CoAuthoredByRegex = /co-authored-by:\s*(?<name>.+)(<(?<email>.+)>)/gim;
100100
const PullRequestRE = /\([ a-z]*(#\d+)\s*\)/gm;
101101
const IssueRE = /(#\d+)/gm;
102-
const RevertHashRE = /This reverts commit (?<hash>[a-f0-9]{40})./gm;
102+
const RevertHashRE = /This reverts commit (?<hash>[\da-f]{40})./gm;
103103

104104
export function parseGitCommit(
105105
commit: RawGitCommit,
@@ -134,10 +134,10 @@ export function parseGitCommit(
134134
description = description.replace(PullRequestRE, "").trim();
135135

136136
// Extract the reverted hashes.
137-
const revertedHashes = []
138-
const matchedHashes = commit.body.matchAll(RevertHashRE)
137+
const revertedHashes = [];
138+
const matchedHashes = commit.body.matchAll(RevertHashRE);
139139
for (const matchedHash of matchedHashes) {
140-
revertedHashes.push(matchedHash.groups.hash)
140+
revertedHashes.push(matchedHash.groups.hash);
141141
}
142142

143143
// Find all authors
@@ -161,46 +161,56 @@ export function parseGitCommit(
161161
};
162162
}
163163

164-
export function filterCommits (commits: GitCommit[], config: ChangelogConfig): GitCommit[] {
164+
export function filterCommits(
165+
commits: GitCommit[],
166+
config: ChangelogConfig
167+
): GitCommit[] {
165168
const commitsWithNoDeps = commits.filter(
166169
(c) =>
167-
config.types[c.type] &&
168-
!(c.type === "chore" && c.scope === "deps" && !c.isBreaking)
170+
config.types[c.type] &&
171+
!(c.type === "chore" && c.scope === "deps" && !c.isBreaking)
169172
);
170173

171-
let resolvedCommits: GitCommit[] = []
172-
let revertWatchList: RevertPair[] = []
174+
let resolvedCommits: GitCommit[] = [];
175+
let revertWatchList: RevertPair[] = [];
173176
for (const commit of commitsWithNoDeps) {
174177
// Include the reverted hashes in the watch list
175178
if (commit.revertedHashes.length > 0) {
176-
revertWatchList.push(...commit.revertedHashes.map(revertedHash => ({
177-
revertedHash,
178-
shortRevertingHash: commit.shortHash
179-
} as RevertPair)))
179+
revertWatchList.push(
180+
...commit.revertedHashes.map(
181+
(revertedHash) =>
182+
({
183+
revertedHash,
184+
shortRevertingHash: commit.shortHash,
185+
} as RevertPair)
186+
)
187+
);
180188
}
181189

182190
// Find the commits which revert the current commit being evaluated
183-
const shortRevertingHashes = revertWatchList.filter(
184-
pair => pair.revertedHash.startsWith(commit.shortHash)
185-
).map(pair => pair.shortRevertingHash)
191+
const shortRevertingHashes = revertWatchList
192+
.filter((pair) => pair.revertedHash.startsWith(commit.shortHash))
193+
.map((pair) => pair.shortRevertingHash);
186194

187195
if (shortRevertingHashes.length > 0) {
188196
// Remove commits that reverts this current commit
189197
resolvedCommits = resolvedCommits.filter(
190-
resolvedCommit => !shortRevertingHashes.includes(resolvedCommit.shortHash)
191-
)
198+
(resolvedCommit) =>
199+
!shortRevertingHashes.includes(resolvedCommit.shortHash)
200+
);
192201

193202
// Unwatch reverting hashes that has been resolved
194203
revertWatchList = revertWatchList.filter(
195-
watchedRevert => !shortRevertingHashes.includes(watchedRevert.shortRevertingHash)
196-
)
204+
(watchedRevert) =>
205+
!shortRevertingHashes.includes(watchedRevert.shortRevertingHash)
206+
);
197207
} else {
198208
// If the current commit is known not to have been reverted, put it to resolved commits.
199-
resolvedCommits = [...resolvedCommits, commit]
209+
resolvedCommits = [...resolvedCommits, commit];
200210
}
201211
}
202212

203-
return resolvedCommits
213+
return resolvedCommits;
204214
}
205215

206216
async function execCommand(cmd: string, args: string[]) {

test/git.test.ts

Lines changed: 64 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from "vitest";
2-
import { GitCommit } from '../src/git'
3-
import type { ChangelogConfig } from '../src/config'
2+
import { GitCommit } from "../src/git";
3+
import type { ChangelogConfig } from "../src/config";
44
import {
55
generateMarkDown,
66
getGitDiff,
@@ -329,95 +329,91 @@ describe("git", () => {
329329
`);
330330
});
331331

332-
test("filterCommits should retain reverts from previous version", async () => {
332+
test("filterCommits should retain reverts from previous version", () => {
333333
const inputLog = [
334334
{
335-
type: 'example',
336-
scope: '',
337-
shortHash: 'a12345',
338-
revertedHashes: ['b12345']
335+
type: "example",
336+
scope: "",
337+
shortHash: "a12345",
338+
revertedHashes: ["b12345"],
339339
} as unknown as GitCommit,
340340
{
341-
type: 'example',
342-
scope: '',
343-
shortHash: 'c12345',
344-
revertedHashes: ['d12345']
345-
} as unknown as GitCommit
341+
type: "example",
342+
scope: "",
343+
shortHash: "c12345",
344+
revertedHashes: ["d12345"],
345+
} as unknown as GitCommit,
346346
];
347347
const config: ChangelogConfig = {
348348
types: {
349-
example: { title: 'Example' }
349+
example: { title: "Example" },
350350
},
351351
scopeMap: undefined,
352-
github: '',
353-
from: '',
354-
to: '',
355-
cwd: '',
356-
output: ''
357-
}
352+
github: "",
353+
from: "",
354+
to: "",
355+
cwd: "",
356+
output: "",
357+
};
358358

359-
const resolvedLog = filterCommits(inputLog, config)
360-
expect(resolvedLog).toStrictEqual(
361-
[
362-
{
363-
type: 'example',
364-
scope: '',
365-
shortHash: 'a12345',
366-
revertedHashes: ['b12345']
367-
} as unknown as GitCommit,
368-
{
369-
type: 'example',
370-
scope: '',
371-
shortHash: 'c12345',
372-
revertedHashes: ['d12345']
373-
} as unknown as GitCommit
374-
]
375-
);
359+
const resolvedLog = filterCommits(inputLog, config);
360+
expect(resolvedLog).toStrictEqual([
361+
{
362+
type: "example",
363+
scope: "",
364+
shortHash: "a12345",
365+
revertedHashes: ["b12345"],
366+
} as unknown as GitCommit,
367+
{
368+
type: "example",
369+
scope: "",
370+
shortHash: "c12345",
371+
revertedHashes: ["d12345"],
372+
} as unknown as GitCommit,
373+
]);
376374
});
377375

378-
test("filterCommits should remove reverts from upcoming version", async () => {
376+
test("filterCommits should remove reverts from upcoming version", () => {
379377
const inputLog = [
380378
{
381-
type: 'example',
382-
scope: '',
383-
shortHash: 'a12345',
384-
revertedHashes: ['b12345']
379+
type: "example",
380+
scope: "",
381+
shortHash: "a12345",
382+
revertedHashes: ["b12345"],
385383
} as unknown as GitCommit,
386384
{
387-
type: 'example',
388-
scope: '',
389-
shortHash: 'b12345',
390-
revertedHashes: []
385+
type: "example",
386+
scope: "",
387+
shortHash: "b12345",
388+
revertedHashes: [],
391389
} as unknown as GitCommit,
392390
{
393-
type: 'example',
394-
scope: '',
395-
shortHash: 'c12345',
396-
revertedHashes: []
397-
} as unknown as GitCommit
391+
type: "example",
392+
scope: "",
393+
shortHash: "c12345",
394+
revertedHashes: [],
395+
} as unknown as GitCommit,
398396
];
399397
const config: ChangelogConfig = {
400398
types: {
401-
example: { title: 'Example' }
399+
example: { title: "Example" },
402400
},
403401
scopeMap: undefined,
404-
github: '',
405-
from: '',
406-
to: '',
407-
cwd: '',
408-
output: ''
409-
}
402+
github: "",
403+
from: "",
404+
to: "",
405+
cwd: "",
406+
output: "",
407+
};
410408

411-
const resolvedLog = filterCommits(inputLog, config)
412-
expect(resolvedLog).toStrictEqual(
413-
[
414-
{
415-
type: 'example',
416-
scope: '',
417-
shortHash: 'c12345',
418-
revertedHashes: []
419-
} as unknown as GitCommit
420-
]
421-
);
409+
const resolvedLog = filterCommits(inputLog, config);
410+
expect(resolvedLog).toStrictEqual([
411+
{
412+
type: "example",
413+
scope: "",
414+
shortHash: "c12345",
415+
revertedHashes: [],
416+
} as unknown as GitCommit,
417+
]);
422418
});
423419
});

0 commit comments

Comments
 (0)