build: use rome as linter+formatter #310

Merged
mcataford merged 1 commit from build/use-rome-instead-of-eslint into main 2023-03-26 15:50:28 +00:00
13 changed files with 432 additions and 2295 deletions
Showing only changes of commit d650690081 - Show all commits

View file

@ -1,13 +0,0 @@
module.exports = {
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: [
"@tophat/eslint-config/base",
"@tophat/eslint-config/jest",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
parserOptions: {
project: ['./tsconfig.eslint.json']
}
}

View file

@ -30,8 +30,8 @@
"prepack": "yarn build", "prepack": "yarn build",
"prebuild": "rm -rf dist", "prebuild": "rm -rf dist",
"build": "tsc --project .", "build": "tsc --project .",
"lint": "eslint tests/**/*.ts src/**/*.ts", "lint": "yarn rome format src tests && yarn rome check src tests",
"lint:fix": "yarn lint --fix", "lint:fix": "yarn rome format src tests --write && yarn rome check src tests --apply",
"test": "jest tests", "test": "jest tests",
"test:watch": "yarn test --watchAll", "test:watch": "yarn test --watchAll",
"test:coverage": "yarn test --coverage", "test:coverage": "yarn test --coverage",
@ -39,19 +39,10 @@
"types:watch": "yarn types --watch" "types:watch": "yarn types --watch"
}, },
"devDependencies": { "devDependencies": {
"@tophat/eslint-config": "^6.0.1",
"@types/jest": "^29.5.0", "@types/jest": "^29.5.0",
"@types/node": "^18.15.5", "@types/node": "^18.15.5",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"eslint": "^8.36.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-node": "^0.3.7",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.5.0", "jest": "^29.5.0",
"prettier": "^2.8.5", "rome": "^11.0.0",
"ts-jest": "^29.0.5", "ts-jest": "^29.0.5",
"typescript": "^4.3.0" "typescript": "^4.3.0"
} }

13
rome.json Normal file
View file

@ -0,0 +1,13 @@
{
"formatter": {
"enabled": true,
"lineWidth": 120
},
"javascript": {
"formatter": {
"semicolons": "asNeeded",
"quoteStyle": "single"
}
}
}

View file

@ -3,20 +3,13 @@ import { join, resolve } from 'path'
import { assertInPackageRoot } from './invariants' import { assertInPackageRoot } from './invariants'
import logger from './logger' import logger from './logger'
import { import { createOrUpdateManifest, getCurrentPackageStats, getPreviousPackageStats, mergeDefaultArguments } from './utils'
createOrUpdateManifest,
getCurrentPackageStats,
getPreviousPackageStats,
mergeDefaultArguments,
} from './utils'
import type { PackwatchArguments } from './types' import type { PackwatchArguments } from './types'
const MANIFEST_FILENAME = '.packwatch.json' const MANIFEST_FILENAME = '.packwatch.json'
export default async function packwatch( export default async function packwatch(args: Partial<PackwatchArguments>): Promise<void> {
args: Partial<PackwatchArguments>,
): Promise<void> {
const { cwd, isUpdatingManifest } = mergeDefaultArguments(args) const { cwd, isUpdatingManifest } = mergeDefaultArguments(args)
const manifestPath = resolve(join(cwd, MANIFEST_FILENAME)) const manifestPath = resolve(join(cwd, MANIFEST_FILENAME))
@ -47,12 +40,7 @@ export default async function packwatch(
const previousStats = getPreviousPackageStats(cwd) const previousStats = getPreviousPackageStats(cwd)
const { packageSizeBytes, packageSize } = currentStats const { packageSizeBytes, packageSize } = currentStats
const { const { packageSize: previousSize, packageSizeBytes: previousSizeBytes, limit, limitBytes } = previousStats
packageSize: previousSize,
packageSizeBytes: previousSizeBytes,
limit,
limitBytes,
} = previousStats
const hasExceededLimit = limitBytes && packageSizeBytes > limitBytes const hasExceededLimit = limitBytes && packageSizeBytes > limitBytes
/* /*
@ -66,9 +54,7 @@ export default async function packwatch(
updateLimit: true, updateLimit: true,
manifestPath, manifestPath,
}) })
logger.log( logger.log(`📝 Updated the manifest! Package size: ${packageSize}, Limit: ${packageSize}`)
`📝 Updated the manifest! Package size: ${packageSize}, Limit: ${packageSize}`,
)
return return
} }
@ -91,17 +77,11 @@ export default async function packwatch(
*/ */
if (packageSizeBytes > previousSizeBytes) { if (packageSizeBytes > previousSizeBytes) {
logger.log( logger.log(`📦 👀 Your package grew! ${packageSize} > ${previousSize} (Limit: ${limit})`)
`📦 👀 Your package grew! ${packageSize} > ${previousSize} (Limit: ${limit})`,
)
} else if (packageSizeBytes < previousSizeBytes) { } else if (packageSizeBytes < previousSizeBytes) {
logger.log( logger.log(`📦 💯 Your package shrank! ${packageSize} < ${previousSize} (Limit: ${limit})`)
`📦 💯 Your package shrank! ${packageSize} < ${previousSize} (Limit: ${limit})`,
)
} else { } else {
logger.log( logger.log(`📦 Nothing to report! Your package is the same size as the latest manifest reports! (Limit: ${limit})`)
`📦 Nothing to report! Your package is the same size as the latest manifest reports! (Limit: ${limit})`,
)
} }
return return
} }

View file

@ -8,9 +8,7 @@ export function assertInPackageRoot(cwd: string): void {
const packageJsonExists = existsSync(packagePath) const packageJsonExists = existsSync(packagePath)
if (!packageJsonExists) { if (!packageJsonExists) {
logger.log( logger.log('🤔 There is no package.json file here. Are you in the root directory of your project?')
'🤔 There is no package.json file here. Are you in the root directory of your project?',
)
throw new Error('NOT_IN_PACKAGE_ROOT') throw new Error('NOT_IN_PACKAGE_ROOT')
} }
} }

View file

@ -11,9 +11,7 @@ const SIZE_MAGNITUDE_PATT = /([0-9]+\.?[0-9]*)/
const MANIFEST_FILENAME = '.packwatch.json' const MANIFEST_FILENAME = '.packwatch.json'
export function mergeDefaultArguments( export function mergeDefaultArguments(args: Partial<PackwatchArguments>): PackwatchArguments {
args: Partial<PackwatchArguments>,
): PackwatchArguments {
return { return {
cwd: args.cwd ?? '.', cwd: args.cwd ?? '.',
isUpdatingManifest: args.isUpdatingManifest ?? false, isUpdatingManifest: args.isUpdatingManifest ?? false,

View file

@ -23,9 +23,7 @@ async function prepareWorkspace(): Promise<string> {
} }
async function cleanUpWorkspace(paths: string[]): Promise<void> { async function cleanUpWorkspace(paths: string[]): Promise<void> {
await Promise.all( await Promise.all(paths.map(async (path) => fs.rmdir(path, { recursive: true })))
paths.map(async (path) => fs.rmdir(path, { recursive: true })),
)
} }
async function createFile(path: string, content: string): Promise<void> { async function createFile(path: string, content: string): Promise<void> {
@ -34,16 +32,10 @@ async function createFile(path: string, content: string): Promise<void> {
async function createPackageJson(cwd: string): Promise<void> { async function createPackageJson(cwd: string): Promise<void> {
const path = resolve(join(cwd, 'package.json')) const path = resolve(join(cwd, 'package.json'))
await createFile( await createFile(path, '{ "name": "wow", "version": "0.0.0", "files": ["!.packwatch.json"] }')
path,
'{ "name": "wow", "version": "0.0.0", "files": ["!.packwatch.json"] }',
)
} }
async function createManifest( async function createManifest(cwd: string, configuration: Report): Promise<void> {
cwd: string,
configuration: Report,
): Promise<void> {
const path = resolve(join(cwd, '.packwatch.json')) const path = resolve(join(cwd, '.packwatch.json'))
await createFile(path, JSON.stringify(configuration)) await createFile(path, JSON.stringify(configuration))
} }
@ -63,15 +55,11 @@ describe('Packwatch', () => {
const workspacePath = await prepareWorkspace() const workspacePath = await prepareWorkspace()
const mockLogger = jest.spyOn(console, 'log') const mockLogger = jest.spyOn(console, 'log')
await expect(async () => await expect(async () => packwatch({ cwd: workspacePath })).rejects.toThrow('NOT_IN_PACKAGE_ROOT')
packwatch({ cwd: workspacePath }),
).rejects.toThrow('NOT_IN_PACKAGE_ROOT')
expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toEqual( expect(mockLogger.mock.calls[0][0]).toEqual(
expect.stringMatching( expect.stringMatching('There is no package.json file here. Are you in the root directory of your project?'),
'There is no package.json file here. Are you in the root directory of your project?',
),
) )
}) })
@ -80,14 +68,9 @@ describe('Packwatch', () => {
const workspacePath = await prepareWorkspace() const workspacePath = await prepareWorkspace()
await createPackageJson(workspacePath) await createPackageJson(workspacePath)
await expect(async () => await expect(async () => packwatch({ cwd: workspacePath })).rejects.toThrow('NO_MANIFEST_NO_UPDATE')
packwatch({ cwd: workspacePath }),
).rejects.toThrow('NO_MANIFEST_NO_UPDATE')
const generatedManifest = await fs.readFile( const generatedManifest = await fs.readFile(resolve(join(workspacePath, '.packwatch.json')), { encoding: 'utf8' })
resolve(join(workspacePath, '.packwatch.json')),
{ encoding: 'utf8' },
)
expect(generatedManifest).toBe( expect(generatedManifest).toBe(
`{"limit":"${actualSize} B","packageSize":"${actualSize} B","unpackedSize":"68 B"}`, `{"limit":"${actualSize} B","packageSize":"${actualSize} B","unpackedSize":"68 B"}`,
@ -100,9 +83,7 @@ describe('Packwatch', () => {
const mockError = jest.spyOn(console, 'error') const mockError = jest.spyOn(console, 'error')
await createPackageJson(workspacePath) await createPackageJson(workspacePath)
await expect(async () => await expect(async () => packwatch({ cwd: workspacePath })).rejects.toThrow()
packwatch({ cwd: workspacePath }),
).rejects.toThrow()
expect(mockWarn.mock.calls).toHaveLength(1) expect(mockWarn.mock.calls).toHaveLength(1)
expect(mockWarn.mock.calls[0][0]).toEqual( expect(mockWarn.mock.calls[0][0]).toEqual(
@ -150,9 +131,7 @@ describe('Packwatch', () => {
await packwatch({ cwd: workspacePath }) await packwatch({ cwd: workspacePath })
expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toEqual( expect(mockLogger.mock.calls[0][0]).toEqual(
expect.stringMatching( expect.stringMatching(/Nothing to report! Your package is the same size as the latest manifest reports!/),
/Nothing to report! Your package is the same size as the latest manifest reports!/,
),
) )
}) })
@ -191,9 +170,7 @@ describe('Packwatch', () => {
await packwatch({ cwd: workspacePath }) await packwatch({ cwd: workspacePath })
expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toEqual( expect(mockLogger.mock.calls[0][0]).toEqual(
expect.stringMatching( expect.stringMatching(/Your package grew! \d+ B > 150B \(Limit: 180B\)/),
/Your package grew! \d+ B > 150B \(Limit: 180B\)/,
),
) )
}) })
it('messages when the size is lower than the limit (shrinkage)', async () => { it('messages when the size is lower than the limit (shrinkage)', async () => {
@ -211,9 +188,7 @@ describe('Packwatch', () => {
await packwatch({ cwd: workspacePath }) await packwatch({ cwd: workspacePath })
expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toEqual( expect(mockLogger.mock.calls[0][0]).toEqual(
expect.stringMatching( expect.stringMatching(/Your package shrank! \d+ B < 170B \(Limit: 180B\)/),
/Your package shrank! \d+ B < 170B \(Limit: 180B\)/,
),
) )
}) })
it('messages when the size exceeds the limit', async () => { it('messages when the size exceeds the limit', async () => {
@ -228,9 +203,7 @@ describe('Packwatch', () => {
unpackedSizeBytes: 140, unpackedSizeBytes: 140,
}) })
await expect(async () => await expect(async () => packwatch({ cwd: workspacePath })).rejects.toThrow('PACKAGE_EXCEEDS_LIMIT')
packwatch({ cwd: workspacePath }),
).rejects.toThrow('PACKAGE_EXCEEDS_LIMIT')
expect(mockError.mock.calls).toHaveLength(1) expect(mockError.mock.calls).toHaveLength(1)
expect(mockError.mock.calls[0][0]).toEqual( expect(mockError.mock.calls[0][0]).toEqual(
expect.stringMatching( expect.stringMatching(
@ -254,9 +227,7 @@ describe('Packwatch', () => {
await packwatch({ cwd: workspacePath, isUpdatingManifest: true }) await packwatch({ cwd: workspacePath, isUpdatingManifest: true })
expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toEqual( expect(mockLogger.mock.calls[0][0]).toEqual(
expect.stringMatching( expect.stringMatching(/Updated the manifest! Package size: \d+ B, Limit: \d+ B/),
/Updated the manifest! Package size: \d+ B, Limit: \d+ B/,
),
) )
}) })
}) })

View file

@ -6,10 +6,7 @@ describe('utils', () => {
${'1 B'} | ${1} ${'1 B'} | ${1}
${'1 kB'} | ${1000} ${'1 kB'} | ${1000}
${'1 mB'} | ${1000000} ${'1 mB'} | ${1000000}
`( `('converts sizes properly ($initialSize -> $expectedSize)', ({ initialSize, expectedSize }) => {
'converts sizes properly ($initialSize -> $expectedSize)',
({ initialSize, expectedSize }) => {
expect(convertSizeToBytes(initialSize)).toEqual(expectedSize) expect(convertSizeToBytes(initialSize)).toEqual(expectedSize)
}, })
)
}) })

View file

@ -1,7 +0,0 @@
{
"extends": "./tsconfig.json",
"include": ["src", "tests", ".eslintrc.js"],
"exclude": [
"dist/**/*"
]
}

1941
yarn.lock

File diff suppressed because it is too large Load diff