Skip to content

chore: experiment with biomejs #7134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions biome.codegen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"$schema": "https://biomejs.dev/schemas/2.0.0/schema.json",
"files": {
"maxSize": 16000000,
"includes": [
"{clients,private}/**/*.{mjs,js,ts,json}",
"!**/dist-{cjs,es,types}/**",
"!**/ruleset.ts"
]
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"lineWidth": 120,
"indentWidth": 2,
"lineEnding": "lf",
"bracketSpacing": true,
"bracketSameLine": false
},
"assist": {
"enabled": true,
"actions": {
"source": {
"organizeImports": "on"
}
}
},
"linter": {
"rules": {
"recommended": true,
"complexity": {
"noForEach": "off"
},
"correctness": {
"noUndeclaredVariables": "off",
"noUnusedVariables": "off",
"noInvalidBuiltinInstantiation": "error"
},
"style": {
"noNamespace": "error",
"useConsistentArrayType": {
"level": "off",
"options": {
"syntax": "shorthand"
}
},
"noParameterAssign": "off",
"useAsConstAssertion": "off",
"useDefaultParameterLast": "off",
"useEnumInitializers": "error",
"useSingleVarDeclarator": "off",
"noUnusedTemplateLiteral": "off",
"useNumberNamespace": "off",
"noInferrableTypes": "off",
"noUselessElse": "off"
},
"nursery": {
"useIterableCallbackReturn": "off"
},
"suspicious": {
"noEmptyBlockStatements": "off",
"noExplicitAny": "off",
"noImplicitAnyLet": "off",
"noEmptyBlock": "off",
"noEmptyInterface": "off",
"noAssignInExpressions": "off",
"noSparseArray": "off"
}
}
},
"javascript": {
"formatter": {
"trailingCommas": "es5"
}
},
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
}
}
83 changes: 83 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"$schema": "https://biomejs.dev/schemas/2.0.0/schema.json",
"files": {
"maxSize": 16000000,
"includes": [
"{packages,lib,scripts,tests}/**/*.{mjs,js,ts,json}",
"!**/dist-{cjs,es,types}/**",
"!**/ruleset.ts"
]
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"lineWidth": 120,
"indentWidth": 2,
"lineEnding": "lf",
"bracketSpacing": true,
"bracketSameLine": false
},
"assist": {
"enabled": true,
"actions": {
"source": {
"organizeImports": "on"
}
}
},
"linter": {
"rules": {
"recommended": true,
"complexity": {
"noForEach": "off"
},
"correctness": {
"noUndeclaredVariables": "off",
"noUnusedVariables": "info",
"noInvalidBuiltinInstantiation": "error",
"noSwitchDeclarations": "info"
},
"style": {
"noNamespace": "error",
"useConsistentArrayType": {
"level": "info",
"options": {
"syntax": "shorthand"
}
},
"noParameterAssign": "info",
"useAsConstAssertion": "info",
"useDefaultParameterLast": "info",
"useEnumInitializers": "error",
"useSingleVarDeclarator": "error",
"noUnusedTemplateLiteral": "info",
"useNumberNamespace": "info",
"noInferrableTypes": "info",
"noUselessElse": "info"
},
"nursery": {
"useIterableCallbackReturn": "off"
},
"suspicious": {
"noEmptyBlockStatements": "info",
"noExplicitAny": "info",
"noImplicitAnyLet": "info",
"noEmptyBlock": "info",
"noEmptyInterface": "info",
"noAssignInExpressions": "info",
"noSparseArray": "info",
"noShadowRestrictedNames": "info"
}
}
},
"javascript": {
"formatter": {
"trailingCommas": "es5"
}
},
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
}
}
23 changes: 8 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"generate:clients:generic": "node ./scripts/generate-clients/generic",
"generate:defaults-mode-provider": "./scripts/generate-defaults-mode-provider/index.js",
"lerna:version": "lerna version --exact --conventional-commits --no-push --no-git-tag-version --no-commit-hooks --loglevel silent --yes",
"lint:ci": "lerna exec --since origin/main --exclude-dependents --ignore '@aws-sdk/client-*' --ignore '@aws-sdk/aws-*' 'eslint --quiet src/**/*.ts'",
"lint:release": "lerna exec --ignore '@aws-sdk/client-*' --ignore '@aws-sdk/aws-*' 'eslint --quiet src/**/*.ts'",
"lint": "node scripts/biome/run-biome.mjs",
"lint:ci": "node scripts/biome/run-biome.mjs",
"lint:release": "node scripts/biome/run-biome.mjs",
"lint:versions": "node scripts/runtime-dependency-version-check/runtime-dep-version-check.js",
"lint:dependencies": "node scripts/runtime-dependency-version-check/check-dependencies.js",
"local-publish": "node ./scripts/verdaccio-publish/index.js",
Expand Down Expand Up @@ -62,30 +63,24 @@
},
"license": "UNLICENSED",
"devDependencies": {
"@biomejs/biome": "2.0.0",
"@biomejs/js-api": "1.0.0",
"@biomejs/wasm-nodejs": "2.0.0",
"@commitlint/cli": "17.0.2",
"@commitlint/config-conventional": "17.0.2",
"@cucumber/cucumber": "8.5.3",
"@cucumber/pretty-formatter": "^1.0.0",
"@fastify/formbody": "^7.4.0",
"@microsoft/api-extractor": "7.52.7",
"@mixer/parallel-prettier": "2.0.3",
"@tsconfig/recommended": "1.0.1",
"@types/fs-extra": "^8.0.1",
"@types/jest": "29.5.11",
"@types/jsdom": "20.0.1",
"@typescript-eslint/eslint-plugin": "5.55.0",
"@typescript-eslint/parser": "5.55.0",
"async": "3.2.4",
"concurrently": "7.0.0",
"decomment": "0.9.5",
"downlevel-dts": "0.10.1",
"esbuild": "0.25.0",
"eslint": "8.36.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-simple-import-sort": "7.0.0",
"eslint-plugin-sort-export-all": "1.2.2",
"eslint-plugin-tsdoc": "0.2.17",
"esprint": "3.6.0",
"fastify": "^4.11.0",
"figlet": "^1.5.0",
Expand All @@ -100,7 +95,6 @@
"kill-port": "^2.0.1",
"lerna": "5.5.2",
"lint-staged": "^10.0.1",
"prettier": "2.8.5",
"rimraf": "3.0.2",
"ts-jest": "29.1.1",
"ts-loader": "9.4.2",
Expand Down Expand Up @@ -135,10 +129,9 @@
},
"lint-staged": {
"{lib,packages}/**/src/**/*.ts": [
"eslint --fix",
"prettier --write"
"echo yarn @biomejs/biome check --write"
],
"**/*.{ts,js,md,json}": "prettier --write"
"**/*.{ts,js,md,json}": "echo yarn @biomejs/biome check --write"
},
"packageManager": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { execa as exec } from "execa";
import { promises as fsPromise } from "fs";
import { join } from "path";
import prettier from "prettier";

import { PackageContext } from "../load-test-scope";
import type { PackageSizeReportOptions } from "./index";
Expand All @@ -14,9 +13,9 @@ export const generateProject = async (projectDir: string, options: PackageSizeRe
};
for (const [name, template] of Object.entries(options.templates)) {
const filePath = join(projectDir, name);
const file = prettier.format(template(contextWithPeerDep), {
filepath: filePath,
});
const { formatCode } = await import("../../../biome/biome.mjs");
const file = await formatCode(template(contextWithPeerDep), filePath);

await fsPromise.writeFile(filePath, file);
}

Expand Down
79 changes: 79 additions & 0 deletions scripts/biome/biome.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { promises, readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { Biome, Distribution } from "@biomejs/js-api";
import walk from "../utils/walk.js";

const __dirname = dirname(fileURLToPath(import.meta.url));

async function loadBiomeWithConfig(biomeConfigPath) {
const biome = await Biome.create({
distribution: Distribution.NODE,
});
const config = JSON.parse(readFileSync(biomeConfigPath, "utf-8"));
const { projectKey } = biome.openProject(biomeConfigPath);
biome.applyConfiguration(projectKey, config);
return {
biome,
projectKey,
};
}

const coreInstance = await loadBiomeWithConfig(join(__dirname, "..", "..", "biome.json"));
const codegenInstance = await loadBiomeWithConfig(join(__dirname, "..", "..", "biome.codegen.json"));

const skip = /browser-build|node_modules|dist-(es|cjs|types)|ruleset\.ts$|endpoints-ruleset\/temp/;
const match = /\.(m?js|ts|json)$/;

export async function checkDir(dir, { lint = false, codegen = false } = {}) {
const concurrency = [];

for await (const file of walk(dir, ["node_modules"])) {
if (file.match(skip)) {
continue;
}
if (file.match(match)) {
concurrency.push(
promises
.readFile(file, "utf-8")
.then((contents) => {
const formatted = formatCode(contents, file, { codegen });
if (formatted) {
if (lint) {
const linted = lintCode(formatted, file, { codegen });
return promises.writeFile(file, linted, "utf-8");
}
return promises.writeFile(file, formatted, "utf-8");
}
return Promise.resolve();
})
.catch(console.error)
);
}
}

await Promise.all(concurrency);
}

export function formatCode(code, filePath, { codegen } = {}) {
const { biome, projectKey } = codegen ? codegenInstance : coreInstance;
const format = biome.formatContent(projectKey, code, {
filePath,
});
return format.content;
}

export function lintCode(code, filePath, { codegen } = {}) {
const { biome, projectKey } = codegen ? codegenInstance : coreInstance;
const lint = biome.lintContent(projectKey, code, {
filePath,
});
for (const diagnostic of lint.diagnostics) {
if (diagnostic.severity === "error" || diagnostic.severity === "fatal") {
const { category, severity, description, location } = diagnostic;
console.log(`${category} - ${severity}\n\t${description}`);
console.log(`\t\t${location.path.file.split("aws-sdk-js-v3")[1]}:${location.span.join(":")}`);
}
}
return lint.content;
}
10 changes: 10 additions & 0 deletions scripts/biome/repair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { spawnProcess } = require("../utils/spawn-process");

const path = require("node:path");
const fs = require("node:fs");

const root = path.join(__dirname, "..", "..");

if (fs.existsSync(path.join(root, "node_modules", "@biomejs", ""))) {
//
}
34 changes: 34 additions & 0 deletions scripts/biome/run-biome.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { checkDir } from "./biome.mjs";

const __dirname = dirname(fileURLToPath(import.meta.url));

const timings = {};
const formatTime = (ms) => {
return `${(ms / 1000).toFixed(2)}s`;
};

timings.start = performance.now();
console.log("formatting and linting scripts/lib/packages");
await checkDir(join(__dirname, "..", "..", "scripts"), { lint: true });
await checkDir(join(__dirname, "..", "..", "lib"), { lint: true });
await checkDir(join(__dirname, "..", "..", "packages"), { lint: true });
timings.lintCore = performance.now();
console.log("\t", formatTime(timings.lintCore - timings.start));

console.log("linting sample clients (s3/dynamodb)");
await checkDir(join(__dirname, "..", "..", "clients", "client-s3"), { lint: true, codegen: true });
await checkDir(join(__dirname, "..", "..", "clients", "client-dynamodb"), { lint: true, codegen: true });
timings.lintSampleClients = performance.now();
console.log("\t", formatTime(timings.lintSampleClients - timings.lintCore));

console.log("formatting all clients");
await checkDir(join(__dirname, "..", "..", "clients"), { lint: false, codegen: true });
timings.formatClients = performance.now();
console.log("\t", formatTime(timings.formatClients - timings.lintSampleClients));

console.log("formatting all private packages");
await checkDir(join(__dirname, "..", "..", "private"), { lint: false, codegen: true });
timings.formatPrivate = performance.now();
console.log("\t", formatTime(timings.formatPrivate - timings.formatClients));
4 changes: 2 additions & 2 deletions scripts/downlevel-dts/downlevelWorkspace.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { exec } from "child_process";
import decomment from "decomment";
import { access, readFile, writeFile } from "fs/promises";
import { join } from "path";
import prettier from "prettier";
import { promisify } from "util";

import { getAllFiles } from "./getAllFiles.mjs";
Expand Down Expand Up @@ -42,7 +41,8 @@ export const downlevelWorkspace = async (workspacesDir, workspaceName) => {
const content = await readFile(downlevelTypesFilepath, "utf8");
const decommentedContent = decomment(content);
try {
const formatted = prettier.format(decommentedContent, { parser: "typescript" });
const { formatCode } = await import("../biome/biome.mjs");
const formatted = await formatCode(decommentedContent, downlevelTypesFilepath);
await writeFile(downlevelTypesFilepath, formatted);
} catch (error) {
console.warn(`Failed to format "${downlevelTypesFilepath}". Skipping...`);
Expand Down
Loading
Loading