Skip to content

Commit

Permalink
fix reduce without initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed May 23, 2020
1 parent 307ec8f commit 0551891
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 47 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
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
19 changes: 11 additions & 8 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 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
31 changes: 17 additions & 14 deletions plugins/npm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,26 @@ export async function getChangedPackages({
export function getMonorepoPackage() {
const packages = getPackages(process.cwd());

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

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

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

return greatest;
}, {} as IPackageJSON);
return greatest;
},
{ name: "initial" }
);
}

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

0 comments on commit 0551891

Please sign in to comment.