Skip to content

Commit

Permalink
Merge pull request #1249 from intuit/buggo
Browse files Browse the repository at this point in the history
fix reduce without initial value
  • Loading branch information
hipstersmoothie authored May 23, 2020
2 parents a900fb6 + 4cd69dc commit 672874d
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 40 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@typescript-eslint/no-this-alias": 2,
"@typescript-eslint/no-useless-constructor": 2,
"@typescript-eslint/no-floating-promises": 2,
"@typescript-eslint/prefer-reduce-type-parameter": 2,
"@typescript-eslint/prefer-as-const": 2,
"@typescript-eslint/prefer-for-of": 2,
"@typescript-eslint/prefer-optional-chain": 2,
Expand Down
52 changes: 52 additions & 0 deletions packages/core/src/__tests__/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,58 @@ describe("github", () => {
});
});

describe("getTagNotInBaseBranch", () => {
test("works with no tags, defaults to first commit", async () => {
const gh = new Git(options);
gh.getTags = () => Promise.resolve([]);
expect(await gh.getTagNotInBaseBranch("branch")).toBe(
"0b2af75d8b55c8869cda93d0e5589ad9f2677e18"
);
});

test("finds greatest tag not in base branch", async () => {
const gh = new Git(options);

gh.getTags = (ref: string) => {
if (ref === "origin/master") {
return Promise.resolve(["1.0.0", "1.2.3", "1.4.0"]);
}

return Promise.resolve([
"1.0.0",
"1.2.3",
"1.4.0",
"1.4.0-next",
"1.4.1-next",
]);
};

expect(await gh.getTagNotInBaseBranch("branch")).toBe("1.4.1-next");
});

test("finds first tag not in base branch", async () => {
const gh = new Git(options);

gh.getTags = (ref: string) => {
if (ref === "origin/master") {
return Promise.resolve(["1.0.0", "1.2.3", "1.4.0"]);
}

return Promise.resolve([
"1.0.0",
"1.2.3",
"1.4.0",
"1.4.0-next",
"1.4.1-next",
]);
};

expect(await gh.getTagNotInBaseBranch("branch", { first: true })).toBe(
"1.4.0-next"
);
});
});

test("publish", async () => {
const gh = new Git(options);

Expand Down
19 changes: 11 additions & 8 deletions packages/core/src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,16 @@ export default class Changelog {

/** Create a section in the changelog to with all of the changelog notes organized by change type */
private async createLabelSection(split: ICommitSplit, sections: string[]) {
const changelogTitles = this.options.labels.reduce((titles, label) => {
if (label.changelogTitle) {
titles[label.name] = label.changelogTitle;
}
const changelogTitles = this.options.labels.reduce<Record<string, string>>(
(titles, label) => {
if (label.changelogTitle) {
titles[label.name] = label.changelogTitle;
}

return titles;
}, {} as { [label: string]: string });
return titles;
},
{}
);

const labelSections = await Promise.all(
Object.entries(split).map(async ([label, labelCommits]) => {
Expand Down Expand Up @@ -415,12 +418,12 @@ export default class Changelog {
})
);

const mergedSections = labelSections.reduce(
const mergedSections = labelSections.reduce<Record<string, string[]>>(
(acc, [title, commits]) => ({
...acc,
[title]: [...(acc[title] || []), ...commits],
}),
{} as Record<string, string[]>
{}
);

Object.entries(mergedSections)
Expand Down
21 changes: 12 additions & 9 deletions packages/core/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export default class Git {
subject: commit.rawBody!,
files: (commit.files || []).map((file) => path.resolve(file)),
}))
.reduce((all, commit) => {
.reduce<ICommit[]>((all, commit) => {
// The -m option will list a commit for each merge parent. This
// means two items will have the same hash. We are using -m to get all the changed files
// in a merge commit. The following code combines these repeated hashes into
Expand All @@ -360,7 +360,7 @@ export default class Git {
}

return all;
}, [] as ICommit[]);
}, []);
} catch (error) {
console.log(error);
const tag = error.match(/ambiguous argument '(\S+)\.\.\S+'/);
Expand Down Expand Up @@ -855,13 +855,16 @@ export default class Git {
).reverse();
const branchTags = (await this.getTags(`heads/${branch}`)).reverse();
const comparator = options.first ? lt : gt;
const firstGreatestUnique = branchTags.reduce((result, tag) => {
if (!baseTags.includes(tag) && (!result || comparator(tag, result))) {
return tag;
}
const firstGreatestUnique = branchTags.reduce<string | undefined>(
(result, tag) => {
if (!baseTags.includes(tag) && (!result || comparator(tag, result))) {
return tag;
}

return result;
});
return result;
},
undefined
);

this.logger.verbose.info("Tags found in base branch:", baseTags);
this.logger.verbose.info("Tags found in branch:", branchTags);
Expand All @@ -870,7 +873,7 @@ export default class Git {
firstGreatestUnique
);

return firstGreatestUnique;
return firstGreatestUnique || this.getFirstCommit();
}

/** Get the last tag that isn't in the base branch */
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ export const defaultLabels: ILabelDefinition[] = [

/** Construct a map of label => semver label */
export const getVersionMap = (labels = defaultLabels) =>
labels.reduce((semVer, { releaseType: type, name }) => {
labels.reduce<IVersionLabels>((semVer, { releaseType: type, name }) => {
if (type && (isVersionLabel(type) || type === "none")) {
const list = semVer.get(type) || [];
semVer.set(type, [...list, name]);
}

return semVer;
}, new Map() as IVersionLabels);
}, new Map());

const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
Expand Down Expand Up @@ -239,9 +239,9 @@ export default class Release {
);
const allPrCommitHashes = allPrCommits
.filter(Boolean)
.reduce(
.reduce<string[]>(
(all, pr) => [...all, ...pr.map((subCommit) => subCommit.sha)],
[] as string[]
[]
);
const uniqueCommits = allCommits.filter(
(commit) =>
Expand Down
25 changes: 14 additions & 11 deletions packages/core/src/validate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,22 @@ function reporter<T>(validation: t.Validation<T>) {
});

const otherErrors: string[] = [];
const grouped = errors.reduce((acc, item) => {
if (typeof item === "string") {
otherErrors.push(item);
return acc;
}
const grouped = errors.reduce<Record<string, ConfigOptionError[]>>(
(acc, item) => {
if (typeof item === "string") {
otherErrors.push(item);
return acc;
}

if (!acc[item.path]) {
acc[item.path] = [];
}
if (!acc[item.path]) {
acc[item.path] = [];
}

acc[item.path].push(item);
return acc;
}, {} as Record<string, ConfigOptionError[]>);
acc[item.path].push(item);
return acc;
},
{}
);
const paths = Object.keys(grouped);

return [
Expand Down
4 changes: 2 additions & 2 deletions plugins/all-contributors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ export default class AllContributorsPlugin implements IPlugin {
return;
}

const allContributions = Object.values(extra).reduce(
const allContributions = Object.values(extra).reduce<string[]>(
(all, i) => [...all, ...i],
[] as string[]
[]
);
const unknownTypes = allContributions.filter(
(contribution) =>
Expand Down
18 changes: 12 additions & 6 deletions plugins/npm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,29 @@ export async function getChangedPackages({
export function getMonorepoPackage() {
const packages = getPackages(process.cwd());

return packages.reduce((greatest, subPackage) => {
if (!packages.length) {
return {} as IPackageJSON;
}

const monorepoPackage = packages.reduce((greatest, subPackage) => {
if (subPackage.package.version) {
if (!greatest.version) {
return subPackage.package;
if (!greatest.package.version) {
return subPackage;
}

if (subPackage.package.private) {
return greatest;
}

return gt(greatest.version, subPackage.package.version)
return gt(greatest.package.version, subPackage.package.version)
? greatest
: subPackage.package;
: subPackage;
}

return greatest;
}, {} as IPackageJSON);
});

return monorepoPackage.package;
}

/** Get all of the packages+version in the lerna monorepo */
Expand Down

0 comments on commit 672874d

Please sign in to comment.