Skip to content

Commit

Permalink
style: changes
Browse files Browse the repository at this point in the history
I tried to enabled recommended-type-checked, but somehow ended up just making the arrays consistent.
  • Loading branch information
lishaduck committed Jul 18, 2024
1 parent 23f0357 commit da19f15
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/stylistic-type-checked',
'prettier',
],
overrides: [
Expand All @@ -25,6 +26,7 @@ module.exports = {
'error',
{ allow: ['/package\\.json$'] },
],
'@typescript-eslint/array-type': ['error', { default: 'generic' }],
},
},
// Tests
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/package-json-file-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ async function _writeFixture(withTrailingNewline = false): Promise<string> {
return file
}

function cleanup(...files: string[]): Promise<any> {
return Promise.all(files.map((f) => fsp.unlink(f)))
async function cleanup(...files: Array<string>): Promise<void> {
await Promise.all(files.map((f) => fsp.unlink(f)))

return
}
2 changes: 1 addition & 1 deletion src/__tests__/type-syncer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
IWorkspaceResolverService,
} from '../workspace-resolver'

const descriptors: IPackageTypingDescriptor[] = [
const descriptors: Array<IPackageTypingDescriptor> = [
{
typingsName: 'package1',
codePackageName: 'package1',
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/versioning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {

describe('getClosestMatchingVersion', () => {
it('returns the closest matching version', () => {
const inputVersions: IPackageVersionInfo[] = [
const inputVersions: Array<IPackageVersionInfo> = [
{
containsInternalTypings: false,
version: '1.16.0',
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export async function startCli() {
typeSyncer: asFunction(createTypeSyncer),
})
await run(container.resolve<ITypeSyncer>('typeSyncer'))
} catch (err: any) {
C.error(err)
} catch (err) {
C.error(err as any)
process.exitCode = 1
}
}
Expand Down Expand Up @@ -109,7 +109,7 @@ function renderSyncedFile(file: ISyncedFile) {
: chalk`{green.bold (${file.newTypings.length.toString()} new typings added)}`

const dirName = path.basename(path.dirname(path.resolve(file.filePath)))
const title = chalk`📦 ${file.package.name || dirName} {gray.italic — ${
const title = chalk`📦 ${file.package.name ?? dirName} {gray.italic — ${
file.filePath
}} ${badge}`

Expand Down
2 changes: 1 addition & 1 deletion src/config-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function readCliConfig(flags: ICLIArguments['flags']): ISyncOptions {
const readValues = <T extends string>(
key: string,
validator?: (value: string) => boolean,
): T[] | undefined => {
): Array<T> | undefined => {
const values = flags[key]
return typeof values === 'string'
? values
Expand Down
21 changes: 11 additions & 10 deletions src/fs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ async function assertFile(filePath: string) {
}

async function existsAsync(filePath: string): Promise<boolean> {
return stat(filePath)
.then(() => true)
.catch((err) => {
/* istanbul ignore else */
if (err.code === 'ENOENT') {
return false
}
/* istanbul ignore next */
throw err
})
try {
await stat(filePath)
return true
} catch (err) {
/* istanbul ignore else */
if ((err as { code: string }).code === 'ENOENT') {
return false
}
/* istanbul ignore next */
throw err
}
}
4 changes: 1 addition & 3 deletions src/package-json-file-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export function createPackageJSONFileService(): IPackageJSONService {
writePackageFile: async (filePath, fileContent) => {
const contents = await readFileContents(filePath)
const { indent } = detectIndent(contents)
const trailingNewline = contents.length
? contents[contents.length - 1] === '\n'
: false
const trailingNewline = contents.length ? contents.endsWith('\n') : false
const data = JSON.stringify(
fileContent,
null,
Expand Down
4 changes: 2 additions & 2 deletions src/package-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface IPackageSource {
*
* @param packageName
*/
fetch(packageName: string): Promise<IPackageInfo | null>
fetch(this: void, packageName: string): Promise<IPackageInfo | null>
}

/**
Expand All @@ -33,7 +33,7 @@ export function createPackageSource(): IPackageSource {
* Fetches info about a package, or `null` if not found.
*/
fetch: async (name) => {
const response = await fetch(encodeURI(name)).catch((err: any) => {
const response = await fetch(encodeURI(name)).catch((err) => {
if (err.statusCode === 404) {
return null
}
Expand Down
7 changes: 4 additions & 3 deletions src/type-syncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function createTypeSyncer(
const { ignoreDeps, ignorePackages } = opts

const packageFile =
file || (await packageJSONService.readPackageFile(filePath))
file ?? (await packageJSONService.readPackageFile(filePath))
const allLocalPackages = Object.values(IDependencySection)
.map((dep) => {
const section = getDependenciesBySection(packageFile, dep)
Expand Down Expand Up @@ -241,14 +241,15 @@ function getPackageScope(packageName: string): [string, string] | null {
function getPackagesFromSection(
section: IDependenciesSection,
ignoredSection?: boolean,
ignorePackages?: string[],
): IPackageVersion[] {
ignorePackages?: Array<string>,
): Array<IPackageVersion> {
return filterMap(Object.keys(section), (name) => {
const isTyping = name.startsWith('@types/')

// Never ignore `@types` packages.
if (!isTyping) {
// If it's not a `@types` package, check whether the section or package is ignored.
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- We want to check for false as well.
if (ignoredSection || ignorePackages?.includes(name)) {
return false
}
Expand Down
10 changes: 4 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export interface ISyncOptions {
/**
* Ignore certain deps.
*/
ignoreDeps?: IDependencySection[]
ignoreDeps?: Array<IDependencySection>
/**
* Ignore certain packages.
*/
ignorePackages?: string[]
ignorePackages?: Array<string>
}

/**
Expand All @@ -37,9 +37,7 @@ export interface IPackageFile {
/**
* Section in package.json representing dependencies.
*/
export interface IDependenciesSection {
[packageName: string]: string
}
export type IDependenciesSection = Record<string, string>

/**
* Package + version record, collected from the {"package": "^1.2.3"} sections.
Expand Down Expand Up @@ -107,6 +105,6 @@ export enum IDependencySection {
* CLI arguments.
*/
export interface ICLIArguments {
flags: { [key: string]: boolean | string | undefined }
flags: Record<string, boolean | string | undefined>
args: Array<string>
}
17 changes: 9 additions & 8 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function shrinkObject<T extends object>(source: T): Required<T> {
* @param source An array of objects to merge.
*/
export function mergeObjects<T>(source: Array<T>): T {
return source.reduce((accum: any, next: any) => ({ ...accum, ...next }), {})
return source.reduce((accum, next) => ({ ...accum, ...next }), {} as T)
}

/**
Expand Down Expand Up @@ -91,13 +91,14 @@ export function untyped(name: string): string {
* Orders an object.
* @param source
*/
export function orderObject<
T extends Record<string | number | symbol, unknown>,
>(source: T, comparer?: (a: string, b: string) => number): T {
export function orderObject<T extends Record<string | number | symbol, U>, U>(
source: T,
comparer?: (a: string, b: string) => number,
): T {
const keys = Object.keys(source).sort(comparer)
const result: any = {}
const result: Record<string, U> = {}
for (const key of keys) {
result[key] = (source as any)[key]
result[key] = source[key]
}

return result as T
Expand All @@ -108,10 +109,10 @@ export function orderObject<
*
* @param fn
*/
export function memoizeAsync<U extends any[], V>(
export function memoizeAsync<U extends Array<W>, V, W>(
fn: (...args: U) => Promise<V>,
) {
const cache = new Map<any, Promise<V>>()
const cache = new Map<W, Promise<V>>()

async function run(...args: U): Promise<V> {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface IPackageVersionInfo {
* @param version
*/
export function getClosestMatchingVersion(
availableVersions: IPackageVersionInfo[],
availableVersions: Array<IPackageVersionInfo>,
version: string,
) {
const parsedVersion = parseVersion(version)
Expand All @@ -40,7 +40,7 @@ export function getClosestMatchingVersion(
return true
})

return bestMatch || availableVersions[0]
return bestMatch ?? availableVersions[0]
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/workspace-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type IWorkspacesArray = Array<string>
* - 'packages/*'
* ```
*/
export type IWorkspacesObject = {
export interface IWorkspacesObject {
packages: IWorkspacesArray
}

Expand All @@ -61,7 +61,7 @@ type NpmWorkspacesConfig = IWorkspacesArray
*/
type YarnWorkspacesConfig =
| IWorkspacesArray
| (IWorkspacesObject & { nohoist?: string[] })
| (IWorkspacesObject & { nohoist?: Array<string> })

/**
* The contents of a `pnpm-workspace.yaml` file.
Expand Down

0 comments on commit da19f15

Please sign in to comment.