prettier-plugin-biome 0.0.0-stage → 2.5.1792263015
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -2
- package/dist/index.d.ts +7 -0
- package/dist/index.js +201 -0
- package/package.json +38 -4
package/README.md
CHANGED
|
@@ -1,3 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# prettier-plugin-biome
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Usage
|
|
4
|
+
|
|
5
|
+
```json
|
|
6
|
+
{
|
|
7
|
+
"overrides": [
|
|
8
|
+
{
|
|
9
|
+
"files": ["*.ts"],
|
|
10
|
+
"options": {
|
|
11
|
+
"assist.actions.preset": "recommended",
|
|
12
|
+
"assist.actions.recommended": true,
|
|
13
|
+
"assist.actions.source.noDuplicateClasses": "off",
|
|
14
|
+
"assist.actions.source.organizeImports": "on",
|
|
15
|
+
"assist.actions.source.useSortedAttributes": "off",
|
|
16
|
+
"assist.actions.source.useSortedEnumMembers": "off",
|
|
17
|
+
"assist.actions.source.useSortedInterfaceMembers": "off",
|
|
18
|
+
"assist.actions.source.useSortedKeys": "off",
|
|
19
|
+
"assist.actions.source.useSortedPackageJson": "off",
|
|
20
|
+
"assist.actions.source.useSortedProperties": "off",
|
|
21
|
+
"assist.actions.source.useSortedSelectionSet": "off",
|
|
22
|
+
"assist.actions.source.useSortedTypeFields": "off",
|
|
23
|
+
"assist.enabled": true,
|
|
24
|
+
"formatter.attributePosition": "auto",
|
|
25
|
+
"formatter.bracketSameLine": false,
|
|
26
|
+
"formatter.bracketSpacing": true,
|
|
27
|
+
"formatter.delimiterSpacing": false,
|
|
28
|
+
"formatter.enabled": true,
|
|
29
|
+
"formatter.expand": "auto",
|
|
30
|
+
"formatter.indentStyle": "tab",
|
|
31
|
+
"formatter.indentWidth": 2,
|
|
32
|
+
"formatter.lineEnding": "lf",
|
|
33
|
+
"formatter.lineWidth": 80,
|
|
34
|
+
"formatter.trailingNewline": true,
|
|
35
|
+
"javascript.assist.enabled": true,
|
|
36
|
+
"javascript.formatter.arrowParentheses": "always",
|
|
37
|
+
"javascript.formatter.attributePosition": "auto",
|
|
38
|
+
"javascript.formatter.bracketSameLine": false,
|
|
39
|
+
"javascript.formatter.bracketSpacing": true,
|
|
40
|
+
"javascript.formatter.delimiterSpacing": false,
|
|
41
|
+
"javascript.formatter.enabled": true,
|
|
42
|
+
"javascript.formatter.expand": "auto",
|
|
43
|
+
"javascript.formatter.indentStyle": "tab",
|
|
44
|
+
"javascript.formatter.indentWidth": 2,
|
|
45
|
+
"javascript.formatter.jsxQuoteStyle": "double",
|
|
46
|
+
"javascript.formatter.lineEnding": "lf",
|
|
47
|
+
"javascript.formatter.lineWidth": 80,
|
|
48
|
+
"javascript.formatter.operatorLinebreak": "after",
|
|
49
|
+
"javascript.formatter.quoteProperties": "asNeeded",
|
|
50
|
+
"javascript.formatter.quoteStyle": "double",
|
|
51
|
+
"javascript.formatter.semicolons": "always",
|
|
52
|
+
"javascript.formatter.trailingCommas": "all",
|
|
53
|
+
"javascript.formatter.trailingNewline": true,
|
|
54
|
+
"javascript.linter.enabled": true,
|
|
55
|
+
"linter.enabled": true
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
],
|
|
59
|
+
"plugins": ["prettier-plugin-biome"]
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
npm install -D prettier prettier-plugin-biome
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
npx prettier -w /path/to/your.ts
|
|
69
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
const AST_FORMAT = "biome-ast";
|
|
4
|
+
const BOOLEAN = { category: "biome", description: "Biome option", type: "boolean" };
|
|
5
|
+
const CHOICE = (values) => ({
|
|
6
|
+
category: "biome",
|
|
7
|
+
choices: values.map(value => ({ description: value, value })),
|
|
8
|
+
description: "Biome option",
|
|
9
|
+
type: "choice",
|
|
10
|
+
});
|
|
11
|
+
const INT = { category: "biome", type: "int" };
|
|
12
|
+
const table = {
|
|
13
|
+
"assist.actions.preset": CHOICE(["recommended", "all", "none"]),
|
|
14
|
+
"assist.actions.recommended": BOOLEAN,
|
|
15
|
+
"assist.actions.source.noDuplicateClasses": CHOICE(["on", "off"]),
|
|
16
|
+
"assist.actions.source.organizeImports": CHOICE(["on", "off"]),
|
|
17
|
+
"assist.actions.source.preset": CHOICE(["recommended", "all", "none"]),
|
|
18
|
+
"assist.actions.source.recommended": BOOLEAN,
|
|
19
|
+
"assist.actions.source.useSortedAttributes": CHOICE(["on", "off"]),
|
|
20
|
+
"assist.actions.source.useSortedEnumMembers": CHOICE(["on", "off"]),
|
|
21
|
+
"assist.actions.source.useSortedInterfaceMembers": CHOICE(["on", "off"]),
|
|
22
|
+
"assist.actions.source.useSortedKeys": CHOICE(["on", "off"]),
|
|
23
|
+
"assist.actions.source.useSortedPackageJson": CHOICE(["on", "off"]),
|
|
24
|
+
"assist.actions.source.useSortedProperties": CHOICE(["on", "off"]),
|
|
25
|
+
"assist.actions.source.useSortedSelectionSet": CHOICE(["on", "off"]),
|
|
26
|
+
"assist.actions.source.useSortedTypeFields": CHOICE(["on", "off"]),
|
|
27
|
+
"assist.enabled": BOOLEAN,
|
|
28
|
+
"formatter.attributePosition": CHOICE(["auto", "multiline"]),
|
|
29
|
+
"formatter.bracketSameLine": BOOLEAN,
|
|
30
|
+
"formatter.bracketSpacing": BOOLEAN,
|
|
31
|
+
"formatter.delimiterSpacing": BOOLEAN,
|
|
32
|
+
"formatter.enabled": BOOLEAN,
|
|
33
|
+
"formatter.expand": CHOICE(["auto", "always", "never"]),
|
|
34
|
+
"formatter.indentStyle": CHOICE(["tab", "space"]),
|
|
35
|
+
"formatter.indentWidth": INT,
|
|
36
|
+
"formatter.lineEnding": CHOICE(["lf", "crlf", "cr", "auto"]),
|
|
37
|
+
"formatter.lineWidth": INT,
|
|
38
|
+
"formatter.trailingNewline": BOOLEAN,
|
|
39
|
+
"javascript.assist.enabled": BOOLEAN,
|
|
40
|
+
"javascript.formatter.arrowParentheses": CHOICE(["always", "asNeeded"]),
|
|
41
|
+
"javascript.formatter.attributePosition": CHOICE(["auto", "multiline"]),
|
|
42
|
+
"javascript.formatter.bracketSameLine": BOOLEAN,
|
|
43
|
+
"javascript.formatter.bracketSpacing": BOOLEAN,
|
|
44
|
+
"javascript.formatter.delimiterSpacing": BOOLEAN,
|
|
45
|
+
"javascript.formatter.enabled": BOOLEAN,
|
|
46
|
+
"javascript.formatter.expand": CHOICE(["auto", "always", "never"]),
|
|
47
|
+
"javascript.formatter.indentStyle": CHOICE(["tab", "space"]),
|
|
48
|
+
"javascript.formatter.indentWidth": INT,
|
|
49
|
+
"javascript.formatter.jsxQuoteStyle": CHOICE(["double", "single"]),
|
|
50
|
+
"javascript.formatter.lineEnding": CHOICE(["lf", "crlf", "cr", "auto"]),
|
|
51
|
+
"javascript.formatter.lineWidth": INT,
|
|
52
|
+
"javascript.formatter.operatorLinebreak": CHOICE(["after", "before"]),
|
|
53
|
+
"javascript.formatter.quoteProperties": CHOICE(["asNeeded", "preserve"]),
|
|
54
|
+
"javascript.formatter.quoteStyle": CHOICE(["double", "single"]),
|
|
55
|
+
"javascript.formatter.semicolons": CHOICE(["always", "asNeeded"]),
|
|
56
|
+
"javascript.formatter.trailingCommas": CHOICE(["all", "es5", "none"]),
|
|
57
|
+
"javascript.formatter.trailingNewline": BOOLEAN,
|
|
58
|
+
"javascript.linter.enabled": BOOLEAN,
|
|
59
|
+
"linter.enabled": BOOLEAN,
|
|
60
|
+
};
|
|
61
|
+
const require = createRequire(import.meta.url);
|
|
62
|
+
const projects = new Map();
|
|
63
|
+
let biomeModule;
|
|
64
|
+
function loadBiome() {
|
|
65
|
+
biomeModule ??= require("@biomejs/wasm-nodejs");
|
|
66
|
+
return biomeModule;
|
|
67
|
+
}
|
|
68
|
+
function createConfiguration(options) {
|
|
69
|
+
const configuration = {};
|
|
70
|
+
for (const path of Object.keys(table)) {
|
|
71
|
+
const value = options[path];
|
|
72
|
+
if (value === undefined) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const segments = path.split(".");
|
|
76
|
+
const leaf = segments.pop();
|
|
77
|
+
let node = configuration;
|
|
78
|
+
for (const segment of segments) {
|
|
79
|
+
node[segment] ??= {};
|
|
80
|
+
node = node[segment];
|
|
81
|
+
}
|
|
82
|
+
if (leaf !== undefined) {
|
|
83
|
+
node[leaf] = value;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return configuration;
|
|
87
|
+
}
|
|
88
|
+
function getProject(filePath, configuration) {
|
|
89
|
+
const projectDirectory = path.dirname(filePath);
|
|
90
|
+
const cacheKey = `${projectDirectory}\0${JSON.stringify(configuration)}`;
|
|
91
|
+
const opened = projects.get(cacheKey);
|
|
92
|
+
if (opened) {
|
|
93
|
+
return opened;
|
|
94
|
+
}
|
|
95
|
+
const workspace = new (loadBiome().Workspace)();
|
|
96
|
+
const { projectKey } = workspace.openProject({ openUninitialized: true, path: projectDirectory });
|
|
97
|
+
const { diagnostics } = workspace.updateSettings({ configuration, projectKey, workspaceDirectory: projectDirectory });
|
|
98
|
+
const errors = diagnostics.filter(diagnostic => diagnostic.severity === "error" || diagnostic.severity === "fatal");
|
|
99
|
+
if (errors.length) {
|
|
100
|
+
throw createDiagnosticsError(filePath, "", errors, "biome configuration error");
|
|
101
|
+
}
|
|
102
|
+
const project = { key: projectKey, workspace };
|
|
103
|
+
projects.set(cacheKey, project);
|
|
104
|
+
return project;
|
|
105
|
+
}
|
|
106
|
+
function createDiagnosticsError(filePath, source, diagnostics, prefix) {
|
|
107
|
+
const printer = new (loadBiome().DiagnosticPrinter)(filePath, source);
|
|
108
|
+
try {
|
|
109
|
+
for (const diagnostic of diagnostics) {
|
|
110
|
+
printer.print_simple(diagnostic);
|
|
111
|
+
}
|
|
112
|
+
return new Error(`${prefix}: ${filePath}\n${printer.finish()}`);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
printer.free();
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function format(text, options) {
|
|
120
|
+
const filePath = path.resolve(options.filepath ?? "input.tsx");
|
|
121
|
+
const configuration = createConfiguration(options);
|
|
122
|
+
const { key: projectKey, workspace } = getProject(filePath, configuration);
|
|
123
|
+
workspace.openFile({ content: { content: text, type: "fromClient", version: 0 }, path: filePath, projectKey });
|
|
124
|
+
try {
|
|
125
|
+
const syntax = workspace.pullDiagnostics({ categories: ["syntax"], path: filePath, projectKey });
|
|
126
|
+
if (syntax.errors) {
|
|
127
|
+
throw createDiagnosticsError(filePath, text, syntax.diagnostics, "parse error");
|
|
128
|
+
}
|
|
129
|
+
return workspace.fixFile({
|
|
130
|
+
fixFileMode: "safeFixes",
|
|
131
|
+
path: filePath,
|
|
132
|
+
projectKey,
|
|
133
|
+
ruleCategories: ["syntax", "lint", "action"],
|
|
134
|
+
shouldFormat: true,
|
|
135
|
+
}).code;
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
workspace.closeFile({ path: filePath, projectKey });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const biomeParser = {
|
|
142
|
+
astFormat: AST_FORMAT,
|
|
143
|
+
locEnd: node => node.text.length,
|
|
144
|
+
locStart: () => 0,
|
|
145
|
+
parse: (text, options) => ({
|
|
146
|
+
text: format(text, options),
|
|
147
|
+
type: "BiomeRoot",
|
|
148
|
+
}),
|
|
149
|
+
};
|
|
150
|
+
const languages = [
|
|
151
|
+
{
|
|
152
|
+
extensions: [".cjs", ".js", ".mjs"],
|
|
153
|
+
linguistLanguageId: 183,
|
|
154
|
+
name: "JavaScript",
|
|
155
|
+
parsers: ["biome"],
|
|
156
|
+
tmScope: "source.js",
|
|
157
|
+
vscodeLanguageId: "javascript",
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
extensions: [".jsx"],
|
|
161
|
+
linguistLanguageId: 183,
|
|
162
|
+
name: "JSX",
|
|
163
|
+
parsers: ["biome"],
|
|
164
|
+
tmScope: "source.js.jsx",
|
|
165
|
+
vscodeLanguageId: "javascriptreact",
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
extensions: [".cts", ".mts", ".ts"],
|
|
169
|
+
linguistLanguageId: 378,
|
|
170
|
+
name: "TypeScript",
|
|
171
|
+
parsers: ["biome"],
|
|
172
|
+
tmScope: "source.ts",
|
|
173
|
+
vscodeLanguageId: "typescript",
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
extensions: [".tsx"],
|
|
177
|
+
linguistLanguageId: 94901924,
|
|
178
|
+
name: "TSX",
|
|
179
|
+
parsers: ["biome"],
|
|
180
|
+
tmScope: "source.tsx",
|
|
181
|
+
vscodeLanguageId: "typescriptreact",
|
|
182
|
+
},
|
|
183
|
+
];
|
|
184
|
+
const parsers = {
|
|
185
|
+
"babel": biomeParser,
|
|
186
|
+
"babel-ts": biomeParser,
|
|
187
|
+
"biome": biomeParser,
|
|
188
|
+
"typescript": biomeParser,
|
|
189
|
+
};
|
|
190
|
+
const printers = {
|
|
191
|
+
[AST_FORMAT]: {
|
|
192
|
+
print: (path) => path.getValue().text,
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
const plugin = {
|
|
196
|
+
languages,
|
|
197
|
+
options: table,
|
|
198
|
+
parsers,
|
|
199
|
+
printers,
|
|
200
|
+
};
|
|
201
|
+
export default plugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
|
+
"dependencies": {
|
|
3
|
+
"@biomejs/wasm-nodejs": "2.5.15"
|
|
4
|
+
},
|
|
5
|
+
"description": "A `biome` toolchain for Prettier",
|
|
6
|
+
"devDependencies": {
|
|
7
|
+
"@tsconfig/bun": "latest",
|
|
8
|
+
"@types/bun": "latest",
|
|
9
|
+
"@types/node": "latest",
|
|
10
|
+
"prettier": "^3.0.0",
|
|
11
|
+
"typescript": "^5.0.0"
|
|
12
|
+
},
|
|
13
|
+
"exports": "./dist/index.js",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"biome",
|
|
19
|
+
"javascript",
|
|
20
|
+
"prettier",
|
|
21
|
+
"prettier-plugin",
|
|
22
|
+
"typescript"
|
|
23
|
+
],
|
|
24
|
+
"license": "Unlicense",
|
|
2
25
|
"name": "prettier-plugin-biome",
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"prettier": "^3.0.0"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"directory": "source/prettier-plugin-biome",
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/openhundun/prettier-kit.git"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "rm -rf dist && tsc"
|
|
36
|
+
},
|
|
37
|
+
"type": "module",
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"version": "2.5.1792263015"
|
|
40
|
+
}
|