Modified sql prettier plugin

This commit is contained in:
2025-09-20 19:18:33 +08:00
parent c3670bb8cd
commit 9222a52d91
44 changed files with 1099 additions and 2211 deletions

View File

@@ -1,3 +0,0 @@
*.tgz
*.unopt.wasm
jsr.jsonc

View File

@@ -1,32 +0,0 @@
export function format(input: string, filename: string, config?: LayoutConfig): string;
interface LayoutConfig {
line_width?: number;
line_ending?: "lf" | "crlf";
language_version?: string;
}
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
export type InitOutput = unknown;
// export type SyncInitInput = BufferSource | WebAssembly.Module;
// /**
// * Instantiates the given `module`, which can either be bytes or
// * a precompiled `WebAssembly.Module`.
// *
// * @param {SyncInitInput} module
// *
// * @returns {InitOutput}
// */
// export function initSync(module: SyncInitInput): InitOutput;
/**
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
* for everything else, calls `WebAssembly.instantiate` directly.
*
* @param {InitInput | Promise<InitInput>} module_or_path
*
* @returns {Promise<InitOutput>}
*/
export default function init(module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;

View File

@@ -1,84 +0,0 @@
import { format as dart_fmt, instantiate, invoke } from "./dart_fmt.mjs";
let wasm;
function get_imports() {}
function init_memory() {}
function normalize(module) {
if (!(module instanceof WebAssembly.Module)) {
return new WebAssembly.Module(module);
}
return module;
}
export default async function (input) {
if (wasm !== undefined) return wasm;
if (typeof input === "undefined") {
input = new URL("dart_fmt.wasm", import.meta.url);
}
const imports = get_imports();
if (
typeof input === "string" ||
(typeof Request === "function" && input instanceof Request) ||
(typeof URL === "function" && input instanceof URL)
) {
input = fetch(input);
}
init_memory(imports);
wasm = await load(await input)
.then(normalize)
.then(instantiate);
invoke(wasm);
return wasm;
}
async function load(module) {
if (typeof Response === "function" && module instanceof Response) {
if ("compileStreaming" in WebAssembly) {
try {
return await WebAssembly.compileStreaming(module);
} catch (e) {
if (module.headers.get("Content-Type") != "application/wasm") {
console.warn(
"`WebAssembly.compileStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",
e,
);
} else {
throw e;
}
}
}
return module.arrayBuffer();
}
return module;
}
export function format(source, filename = "stdin.dart", config = {}) {
const options = { lineEnding: "\n" };
if (config.line_width) {
options.pageWidth = config.line_width;
}
if (options.line_ending === "crlf") {
options.lineEnding = "\r\n";
}
if(options.language_version) {
options.languageVersion = options.language_version;
}
const result = dart_fmt(source, filename, JSON.stringify(options));
const err = result[0] === "x";
const output = result.slice(1);
if (err) {
throw new Error(output);
}
return output;
}

View File

@@ -1,10 +0,0 @@
import fs from "node:fs/promises";
import initAsync from "./dart_fmt.js";
const wasm = new URL("./dart_fmt.wasm", import.meta.url);
export default function __wbg_init(init = fs.readFile(wasm)) {
return initAsync(init);
}
export * from "./dart_fmt.js";

View File

@@ -1,8 +0,0 @@
import initAsync from "./dart_fmt.js";
import wasm from "./dart_fmt.wasm?url";
export default function __wbg_init(input = wasm) {
return initAsync(input);
}
export * from "./dart_fmt.js";

View File

@@ -62,11 +62,10 @@ const dartPrinter: Printer<string> = {
}
const text = (path as any).getValue ? (path as any).getValue() : path.node;
const filename = getDartFilename(options.filepath);
const config = getDartConfig(options);
// Format using dart_fmt (synchronous call)
const formatted = format(text, filename, config);
const formatted = format(text, undefined, config);
return formatted.trim();
} catch (error) {
@@ -77,21 +76,6 @@ const dartPrinter: Printer<string> = {
},
};
// Helper function to get appropriate filename for dart_fmt
function getDartFilename(filepath?: string): string {
if (!filepath) {
return 'stdin.dart';
}
const filename = filepath.split(/[/\\]/).pop() || 'main.dart';
// Ensure .dart extension
if (!filename.endsWith('.dart')) {
return 'main.dart';
}
return filename;
}
// Helper function to create Dart config from Prettier options
function getDartConfig(options: any): any {

View File

@@ -0,0 +1,5 @@
cd $(dirname $0)/..
dart compile wasm ./lib/binding.dart -o ./build/dart_fmt.wasm

View File

@@ -1,7 +0,0 @@
cd $(dirname $0)/..
dart compile wasm ./lib/binding.dart -o ./build/dart_fmt.wasm
cp -LR ./extra/. ./build/
./scripts/patch.mjs ./build/dart_fmt.mjs
./scripts/package.mjs ./package.json

View File

@@ -1,16 +0,0 @@
#!/usr/bin/env node
import process from "node:process";
import path from "node:path";
import fs from "node:fs";
const pkg_path = path.resolve(process.cwd(), process.argv[2]);
const pkg_text = fs.readFileSync(pkg_path, { encoding: "utf-8" });
const pkg_json = JSON.parse(pkg_text);
// JSR
const jsr_path = path.resolve(pkg_path, "..", "build", "jsr.jsonc");
pkg_json.name = "@fmt/dart-fmt";
pkg_json.exports = "./dart_fmt.js";
pkg_json.exclude = ["!../build", "*.tgz", ".npmignore"];
fs.writeFileSync(jsr_path, JSON.stringify(pkg_json, null, 4));

View File

@@ -1,13 +0,0 @@
#!/usr/bin/env node
import process from "node:process";
import path from "node:path";
import fs from "node:fs";
const file_path = path.resolve(process.cwd(), process.argv[2]);
let file_text = fs.readFileSync(file_path, { encoding: "utf-8" });
file_text = file_text.replace(`"length": (s) => s.length`, '"length": (s) => s?.length||0');
file_text = file_text.replace('globalThis.format', 'format');
file_text += "\nexport let format;";
fs.writeFileSync(file_path, file_text);