Skip to content

Commit 065a2e0

Browse files
authored
chore: replace prettier with biome (#4765)
1 parent dc37053 commit 065a2e0

10 files changed

Lines changed: 129 additions & 108 deletions

File tree

.pre-commit-config.yaml

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,13 @@ repos:
6161
- id: renovate-config-validator
6262
alias: renovate
6363
args: [--strict]
64-
- repo: https://github.com/rbubley/mirrors-prettier
65-
# keep it before yamllint
66-
rev: v3.6.2
64+
- repo: https://github.com/biomejs/pre-commit
65+
rev: "v2.2.3"
6766
hooks:
68-
- id: prettier
69-
# Temporary excludes so we can gradually normalize the formatting
70-
exclude: >
71-
(?x)^(
72-
.*\.md$|
73-
examples/other/some.j2.yaml|
74-
examples/playbooks/collections/.*|
75-
examples/playbooks/example.yml|
76-
examples/playbooks/invalid-transform.yml|
77-
examples/playbooks/multiline-brackets.*|
78-
examples/playbooks/templates/not-valid.yaml|
79-
examples/playbooks/vars/empty.transformed.yml|
80-
examples/playbooks/vars/empty.yml|
81-
examples/playbooks/with-skip-tag-id.yml|
82-
examples/playbooks/with-umlaut-.*|
83-
examples/yamllint/.*|
84-
src/ansiblelint/schemas/(molecule|tasks|playbook|rulebook|pattern).json|
85-
test/fixtures/formatting-before/.*|
86-
test/schemas/(negative_test|test)/.*\.md|
87-
test/schemas/data/.*|
88-
src/ansiblelint/schemas/ansible-navigator-config.json
89-
)$
90-
always_run: true
91-
additional_dependencies:
92-
- prettier@3.2.4
93-
- prettier-plugin-sort-json@3.1.0
67+
- id: biome-check
68+
name: biome
69+
alias: biome
70+
args: [--unsafe]
9471
- repo: https://github.com/streetsidesoftware/cspell-cli
9572
rev: v9.2.0
9673
hooks:
@@ -177,11 +154,6 @@ repos:
177154
- id: toml-sort-fix
178155
alias: toml
179156

180-
- repo: https://github.com/tox-dev/tox-ini-fmt
181-
rev: 1.6.0
182-
hooks:
183-
- id: tox-ini-fmt
184-
185157
- repo: https://github.com/astral-sh/ruff-pre-commit
186158
rev: v0.12.11
187159
hooks:

.sonarlint/connectedMode.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"sonarCloudOrganization": "ansible",
3-
"projectKey": "ansible_ansible-lint"
2+
"projectKey": "ansible_ansible-lint",
3+
"sonarCloudOrganization": "ansible"
44
}

.vscode/extensions.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"recommendations": [
3+
"biomejs.biome",
34
"Tyriar.sort-lines",
45
"charliermarsh.ruff",
5-
"esbenp.prettier-vscode",
6+
"github.vscode-github-actions",
67
"hbenl.vscode-test-explorer",
78
"ms-python.mypy-type-checker",
89
"ms-python.pylint",
@@ -17,5 +18,8 @@
1718
"timonwong.shellcheck",
1819
"znck.grammarly"
1920
],
20-
"unwantedRecommendations": ["tamasfe.even-better-toml"]
21+
"unwantedRecommendations": [
22+
"esbenp.prettier-vscode",
23+
"tamasfe.even-better-toml"
24+
]
2125
}

.vscode/settings.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
2+
"[json]": {
3+
"editor.defaultFormatter": "biomejs.biome"
4+
},
25
"[markdown]": {
3-
"editor.defaultFormatter": "esbenp.prettier-vscode"
6+
"editor.defaultFormatter": "biomejs.biome"
47
},
58
"[python]": {
69
"editor.codeActionsOnSave": {
@@ -13,14 +16,20 @@
1316
"[toml]": {
1417
"editor.defaultFormatter": "tombi-toml.tombi"
1518
},
19+
"editor.codeActionsOnSave": {
20+
"source.action.useSortedKeys.biome": "explicit",
21+
"source.fixAll.biome": "always",
22+
"source.organizeImports.biome": "explicit"
23+
},
24+
"editor.defaultFormatter": "biomejs.biome",
1625
"editor.formatOnSave": true,
1726
"evenBetterToml.formatter.alignComments": false,
1827
"evenBetterToml.formatter.arrayTrailingComma": true,
1928
"files.exclude": {
20-
"*.egg-info": true,
29+
"__pycache__": true,
2130
".pytest_cache": true,
2231
".tox": true,
23-
"__pycache__": true,
32+
"*.egg-info": true,
2433
"build": true
2534
},
2635
"git.ignoreLimitWarning": true,

biome.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/latest/schema.json",
3+
"assist": {
4+
"actions": {
5+
"source": {
6+
"useSortedKeys": "on"
7+
}
8+
}
9+
},
10+
"files": {
11+
"includes": [
12+
"**",
13+
"!src/ansiblelint/schemas",
14+
"!docs/_static/theme_overrides.css",
15+
"!test/schemas/negative_test",
16+
"!examples/**/pattern.json"
17+
]
18+
},
19+
"formatter": {
20+
"enabled": true,
21+
"indentStyle": "space"
22+
},
23+
"json": {
24+
"formatter": {
25+
"enabled": true,
26+
"indentStyle": "space"
27+
}
28+
},
29+
"vcs": {
30+
"clientKind": "git",
31+
"enabled": true,
32+
"useIgnoreFile": true
33+
}
34+
}

test/schemas/.mocharc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"colors": true,
33
"extensions": ["ts"],
4-
"require": "ts-node/register",
54
"node-option": [
65
"experimental-specifier-resolution=node",
76
"loader=ts-node/esm"
87
],
8+
"require": "ts-node/register",
99
"slow": "500",
1010
"spec": "src/**/*.spec.ts"
1111
}

test/schemas/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
"ts-node": "^10.9.2",
77
"vscode-json-languageservice": "^5.6.1"
88
},
9-
"scripts": {
10-
"compile": "tsc",
11-
"deps": "npx --yes npm-check-updates -u && npm install --ignore-scripts",
12-
"test": "python3 src/rebuild.py && mocha"
13-
},
149
"devDependencies": {
1510
"@types/chai": "^5.2.2",
1611
"@types/js-yaml": "^4.0.9",
@@ -25,5 +20,10 @@
2520
"directories": {
2621
"test": "./src"
2722
},
23+
"scripts": {
24+
"compile": "tsc",
25+
"deps": "npx --yes npm-check-updates -u && npm install --ignore-scripts",
26+
"test": "python3 src/rebuild.py && mocha"
27+
},
2828
"type": "module"
2929
}

test/schemas/src/schema.spec.ts

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import * as path from "path";
2-
import fs from "fs";
3-
import { minimatch } from "minimatch";
4-
import yaml from "js-yaml";
1+
import { spawnSync } from "node:child_process";
2+
import fs from "node:fs";
3+
import * as path from "node:path";
4+
import Ajv2020 from "ajv/dist/2020";
5+
import draft7MetaSchema from "ajv/dist/refs/json-schema-draft-07.json" with {
6+
type: "json",
7+
};
58
import { assert } from "chai";
9+
import yaml from "js-yaml";
10+
import { minimatch } from "minimatch";
611
import stringify from "safe-stable-stringify";
7-
import { spawnSync } from "child_process";
8-
import Ajv2020 from "ajv/dist/2020";
9-
import draft7MetaSchema from "ajv/dist/refs/json-schema-draft-07.json" with { type: "json" };
1012

1113
function ansiRegex({ onlyFirst = false } = {}) {
1214
const pattern = [
@@ -20,17 +22,17 @@ function ansiRegex({ onlyFirst = false } = {}) {
2022
function stripAnsi(data: string) {
2123
if (typeof data !== "string") {
2224
throw new TypeError(
23-
`Expected a \`string\`, got \`${typeof data}\ = ${data}`,
25+
`Expected a \`string\`, got \`${typeof data} = ${data}`,
2426
);
2527
}
2628
return data.replace(ansiRegex(), "");
2729
}
2830

2931
const ajv = new Ajv2020({
30-
strictTypes: false,
31-
strict: false,
32-
inlineRefs: true, // https://github.com/ajv-validator/ajv/issues/1581#issuecomment-832211568
3332
allErrors: true, // https://github.com/ajv-validator/ajv/issues/1581#issuecomment-832211568
33+
inlineRefs: true, // https://github.com/ajv-validator/ajv/issues/1581#issuecomment-832211568
34+
strict: false,
35+
strictTypes: false,
3436
});
3537
ajv.addMetaSchema(draft7MetaSchema);
3638

@@ -44,7 +46,7 @@ const schema_files = fs
4446
.filter((el) => path.extname(el) === ".json");
4547
console.log(`Schemas: ${schema_files}`);
4648

47-
describe("schemas under f/", function () {
49+
describe("schemas under f/", () => {
4850
schema_files.forEach((schema_file) => {
4951
if (
5052
schema_file.startsWith("_") ||
@@ -56,20 +58,21 @@ describe("schemas under f/", function () {
5658
ajv.addSchema(schema_json);
5759
const validator = ajv.compile(schema_json);
5860
if (
59-
schema_json["examples"] == undefined &&
60-
schema_json["x-ansible-lint"] == undefined
61+
schema_json.examples === undefined &&
62+
schema_json["x-ansible-lint"] === undefined
6163
) {
6264
console.error(
6365
`Schema file ${schema_file} is missing an 'examples' or 'x-ansible-lint' key that we need for documenting file matching patterns.`,
6466
);
65-
return process.exit(1);
67+
process.exit(1);
68+
return;
6669
}
67-
describe(schema_file, function () {
70+
describe(schema_file, () => {
6871
const file_path_key =
69-
schema_json["x-ansible-lint"] || schema_json["examples"];
72+
schema_json["x-ansible-lint"] || schema_json.examples;
7073
getTestFiles(file_path_key).forEach(
7174
({ file: test_file, expect_fail }) => {
72-
it(`linting ${test_file} using ${schema_file}`, function () {
75+
it(`linting ${test_file} using ${schema_file}`, () => {
7376
var errors_md = "";
7477
const result = validator(
7578
yaml.load(fs.readFileSync(test_file, "utf8")),
@@ -86,9 +89,9 @@ describe("schemas under f/", function () {
8689
// and breaks usage from inside virtualenvs.
8790
const proc = spawnSync(
8891
`${process.env.VIRTUAL_ENV}/bin/check-jsonschema -v -o json --schemafile f/${schema_file} ${test_file}`,
89-
{ shell: true, encoding: "utf-8", stdio: "pipe" },
92+
{ encoding: "utf-8", shell: true, stdio: "pipe" },
9093
);
91-
if (proc.status != 0) {
94+
if (proc.status !== 0) {
9295
// real errors are sent to stderr due to https://github.com/python-jsonschema/check-jsonschema/issues/88
9396
errors_md += "# check-jsonschema\n\nstdout:\n\n```json\n";
9497
errors_md += stripAnsi(proc.output[1] || "");
@@ -106,8 +109,8 @@ describe("schemas under f/", function () {
106109
fs.writeFileSync(md_filename, errors_md);
107110
} else {
108111
// if no error occurs, we should ensure there is no md file present
109-
fs.unlink(md_filename, function (err) {
110-
if (err && err.code != "ENOENT") {
112+
fs.unlink(md_filename, (err) => {
113+
if (err && err.code !== "ENOENT") {
111114
console.error(`Failed to remove ${md_filename}.`);
112115
}
113116
});
@@ -123,17 +126,18 @@ describe("schemas under f/", function () {
123126
// All /$defs/ that have examples property are assumed to be
124127
// subschemas, "tasks" being the primary such case, which is also used
125128
// for validating separated files.
126-
for (var definition in schema_json["$defs"]) {
127-
if (schema_json["$defs"][definition].examples) {
128-
const subschema_uri = `${schema_json["$id"]}#/$defs/${definition}`;
129+
for (var definition in schema_json.$defs) {
130+
if (schema_json.$defs[definition].examples) {
131+
const subschema_uri = `${schema_json.$id}#/$defs/${definition}`;
129132
const subschema_validator = ajv.getSchema(subschema_uri);
130133
if (!subschema_validator) {
131134
console.error(`Failed to load subschema ${subschema_uri}`);
132-
return process.exit(1);
135+
process.exit(1);
136+
return;
133137
}
134-
getTestFiles(schema_json["$defs"][definition].examples).forEach(
138+
getTestFiles(schema_json.$defs[definition].examples).forEach(
135139
({ file: test_file, expect_fail }) => {
136-
it(`linting ${test_file} using ${subschema_uri}`, function () {
140+
it(`linting ${test_file} using ${subschema_uri}`, () => {
137141
const result = subschema_validator(
138142
yaml.load(fs.readFileSync(test_file, "utf8")),
139143
);
@@ -157,25 +161,23 @@ function getTestFiles(
157161
): { file: string; expect_fail: boolean }[] {
158162
const files = Array.from(
159163
new Set(
160-
globs
161-
.map((glob: any) => minimatch.match(test_files, path.join("**", glob)))
162-
.flat(),
164+
globs.flatMap((glob: any) =>
165+
minimatch.match(test_files, path.join("**", glob)),
166+
),
163167
),
164168
);
165169
const negative_files = Array.from(
166170
new Set(
167-
globs
168-
.map((glob: any) =>
169-
minimatch.match(negative_test_files, path.join("**", glob)),
170-
)
171-
.flat(),
171+
globs.flatMap((glob: any) =>
172+
minimatch.match(negative_test_files, path.join("**", glob)),
173+
),
172174
),
173175
);
174176

175177
// All fails ending with fail, like `foo.fail.yml` are expected to fail validation
176-
let result = files.map((f) => ({ file: f, expect_fail: false }));
178+
let result = files.map((f) => ({ expect_fail: false, file: f }));
177179
result = result.concat(
178-
negative_files.map((f) => ({ file: f, expect_fail: true })),
180+
negative_files.map((f) => ({ expect_fail: true, file: f })),
179181
);
180182
return result;
181183
}

0 commit comments

Comments
 (0)