Added dart prettier plugin

This commit is contained in:
2025-09-19 19:41:48 +08:00
parent c9379f0edb
commit 2ea3456ff7
11 changed files with 644 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
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

@@ -0,0 +1,84 @@
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

@@ -0,0 +1,350 @@
// `modulePromise` is a promise to the `WebAssembly.module` object to be
// instantiated.
// `importObjectPromise` is a promise to an object that contains any additional
// imports needed by the module that aren't provided by the standard runtime.
// The fields on this object will be merged into the importObject with which
// the module will be instantiated.
// This function returns a promise to the instantiated module.
export const instantiate = async (modulePromise, importObjectPromise) => {
let dartInstance;
// Prints to the console
function printToConsole(value) {
if (typeof dartPrint == "function") {
dartPrint(value);
return;
}
if (typeof console == "object" && typeof console.log != "undefined") {
console.log(value);
return;
}
if (typeof print == "function") {
print(value);
return;
}
throw "Unable to print message: " + js;
}
// Converts a Dart List to a JS array. Any Dart objects will be converted, but
// this will be cheap for JSValues.
function arrayFromDartList(constructor, list) {
const exports = dartInstance.exports;
const read = exports.$listRead;
const length = exports.$listLength(list);
const array = new constructor(length);
for (let i = 0; i < length; i++) {
array[i] = read(list, i);
}
return array;
}
// A special symbol attached to functions that wrap Dart functions.
const jsWrappedDartFunctionSymbol = Symbol("JSWrappedDartFunction");
function finalizeWrapper(dartFunction, wrapped) {
wrapped.dartFunction = dartFunction;
wrapped[jsWrappedDartFunctionSymbol] = true;
return wrapped;
}
// Imports
const dart2wasm = {
_49: v => v.toString(),
_64: s => {
if (!/^\s*[+-]?(?:Infinity|NaN|(?:\.\d+|\d+(?:\.\d*)?)(?:[eE][+-]?\d+)?)\s*$/.test(s)) {
return NaN;
}
return parseFloat(s);
},
_65: () => {
let stackString = new Error().stack.toString();
let frames = stackString.split('\n');
let drop = 2;
if (frames[0] === 'Error') {
drop += 1;
}
return frames.slice(drop).join('\n');
},
_69: () => {
// On browsers return `globalThis.location.href`
if (globalThis.location != null) {
return globalThis.location.href;
}
return null;
},
_70: () => {
return typeof process != "undefined" &&
Object.prototype.toString.call(process) == "[object process]" &&
process.platform == "win32"
},
_85: s => JSON.stringify(s),
_86: s => printToConsole(s),
_87: a => a.join(''),
_90: (s, t) => s.split(t),
_91: s => s.toLowerCase(),
_92: s => s.toUpperCase(),
_93: s => s.trim(),
_97: (s, p, i) => s.indexOf(p, i),
_98: (s, p, i) => s.lastIndexOf(p, i),
_100: Object.is,
_101: s => s.toUpperCase(),
_102: s => s.toLowerCase(),
_103: (a, i) => a.push(i),
_113: (a, b) => a == b ? 0 : (a > b ? 1 : -1),
_114: a => a.length,
_116: (a, i) => a[i],
_117: (a, i, v) => a[i] = v,
_120: (o, start, length) => new Uint8Array(o.buffer, o.byteOffset + start, length),
_121: (o, start, length) => new Int8Array(o.buffer, o.byteOffset + start, length),
_122: (o, start, length) => new Uint8ClampedArray(o.buffer, o.byteOffset + start, length),
_123: (o, start, length) => new Uint16Array(o.buffer, o.byteOffset + start, length),
_124: (o, start, length) => new Int16Array(o.buffer, o.byteOffset + start, length),
_125: (o, start, length) => new Uint32Array(o.buffer, o.byteOffset + start, length),
_126: (o, start, length) => new Int32Array(o.buffer, o.byteOffset + start, length),
_129: (o, start, length) => new Float32Array(o.buffer, o.byteOffset + start, length),
_130: (o, start, length) => new Float64Array(o.buffer, o.byteOffset + start, length),
_133: (o) => new DataView(o.buffer, o.byteOffset, o.byteLength),
_137: Function.prototype.call.bind(Object.getOwnPropertyDescriptor(DataView.prototype, 'byteLength').get),
_138: (b, o) => new DataView(b, o),
_140: Function.prototype.call.bind(DataView.prototype.getUint8),
_141: Function.prototype.call.bind(DataView.prototype.setUint8),
_142: Function.prototype.call.bind(DataView.prototype.getInt8),
_143: Function.prototype.call.bind(DataView.prototype.setInt8),
_144: Function.prototype.call.bind(DataView.prototype.getUint16),
_145: Function.prototype.call.bind(DataView.prototype.setUint16),
_146: Function.prototype.call.bind(DataView.prototype.getInt16),
_147: Function.prototype.call.bind(DataView.prototype.setInt16),
_148: Function.prototype.call.bind(DataView.prototype.getUint32),
_149: Function.prototype.call.bind(DataView.prototype.setUint32),
_150: Function.prototype.call.bind(DataView.prototype.getInt32),
_151: Function.prototype.call.bind(DataView.prototype.setInt32),
_156: Function.prototype.call.bind(DataView.prototype.getFloat32),
_157: Function.prototype.call.bind(DataView.prototype.setFloat32),
_158: Function.prototype.call.bind(DataView.prototype.getFloat64),
_159: Function.prototype.call.bind(DataView.prototype.setFloat64),
_165: x0 => format = x0,
_166: f => finalizeWrapper(f, function(x0,x1,x2) { return dartInstance.exports._166(f,arguments.length,x0,x1,x2) }),
_184: (c) =>
queueMicrotask(() => dartInstance.exports.$invokeCallback(c)),
_187: (s, m) => {
try {
return new RegExp(s, m);
} catch (e) {
return String(e);
}
},
_188: (x0,x1) => x0.exec(x1),
_189: (x0,x1) => x0.test(x1),
_190: (x0,x1) => x0.exec(x1),
_191: (x0,x1) => x0.exec(x1),
_192: x0 => x0.pop(),
_198: o => o === undefined,
_199: o => typeof o === 'boolean',
_200: o => typeof o === 'number',
_202: o => typeof o === 'string',
_205: o => o instanceof Int8Array,
_206: o => o instanceof Uint8Array,
_207: o => o instanceof Uint8ClampedArray,
_208: o => o instanceof Int16Array,
_209: o => o instanceof Uint16Array,
_210: o => o instanceof Int32Array,
_211: o => o instanceof Uint32Array,
_212: o => o instanceof Float32Array,
_213: o => o instanceof Float64Array,
_214: o => o instanceof ArrayBuffer,
_215: o => o instanceof DataView,
_216: o => o instanceof Array,
_217: o => typeof o === 'function' && o[jsWrappedDartFunctionSymbol] === true,
_220: o => o instanceof RegExp,
_221: (l, r) => l === r,
_222: o => o,
_223: o => o,
_224: o => o,
_225: b => !!b,
_226: o => o.length,
_229: (o, i) => o[i],
_230: f => f.dartFunction,
_231: l => arrayFromDartList(Int8Array, l),
_232: (data, length) => {
const jsBytes = new Uint8Array(length);
const getByte = dartInstance.exports.$uint8ListGet;
for (let i = 0; i < length; i++) {
jsBytes[i] = getByte(data, i);
}
return jsBytes;
},
_233: l => arrayFromDartList(Uint8ClampedArray, l),
_234: l => arrayFromDartList(Int16Array, l),
_235: l => arrayFromDartList(Uint16Array, l),
_236: l => arrayFromDartList(Int32Array, l),
_237: l => arrayFromDartList(Uint32Array, l),
_238: l => arrayFromDartList(Float32Array, l),
_239: l => arrayFromDartList(Float64Array, l),
_240: (data, length) => {
const read = dartInstance.exports.$byteDataGetUint8;
const view = new DataView(new ArrayBuffer(length));
for (let i = 0; i < length; i++) {
view.setUint8(i, read(data, i));
}
return view;
},
_241: l => arrayFromDartList(Array, l),
_242: (s, length) => {
if (length == 0) return '';
const read = dartInstance.exports.$stringRead1;
let result = '';
let index = 0;
const chunkLength = Math.min(length - index, 500);
let array = new Array(chunkLength);
while (index < length) {
const newChunkLength = Math.min(length - index, 500);
for (let i = 0; i < newChunkLength; i++) {
array[i] = read(s, index++);
}
if (newChunkLength < chunkLength) {
array = array.slice(0, newChunkLength);
}
result += String.fromCharCode(...array);
}
return result;
}
,
_243: (s, length) => {
if (length == 0) return '';
const read = dartInstance.exports.$stringRead2;
let result = '';
let index = 0;
const chunkLength = Math.min(length - index, 500);
let array = new Array(chunkLength);
while (index < length) {
const newChunkLength = Math.min(length - index, 500);
for (let i = 0; i < newChunkLength; i++) {
array[i] = read(s, index++);
}
if (newChunkLength < chunkLength) {
array = array.slice(0, newChunkLength);
}
result += String.fromCharCode(...array);
}
return result;
}
,
_244: (s) => {
let length = s.length;
let range = 0;
for (let i = 0; i < length; i++) {
range |= s.codePointAt(i);
}
const exports = dartInstance.exports;
if (range < 256) {
if (length <= 10) {
if (length == 1) {
return exports.$stringAllocate1_1(s.codePointAt(0));
}
if (length == 2) {
return exports.$stringAllocate1_2(s.codePointAt(0), s.codePointAt(1));
}
if (length == 3) {
return exports.$stringAllocate1_3(s.codePointAt(0), s.codePointAt(1), s.codePointAt(2));
}
if (length == 4) {
return exports.$stringAllocate1_4(s.codePointAt(0), s.codePointAt(1), s.codePointAt(2), s.codePointAt(3));
}
if (length == 5) {
return exports.$stringAllocate1_5(s.codePointAt(0), s.codePointAt(1), s.codePointAt(2), s.codePointAt(3), s.codePointAt(4));
}
if (length == 6) {
return exports.$stringAllocate1_6(s.codePointAt(0), s.codePointAt(1), s.codePointAt(2), s.codePointAt(3), s.codePointAt(4), s.codePointAt(5));
}
if (length == 7) {
return exports.$stringAllocate1_7(s.codePointAt(0), s.codePointAt(1), s.codePointAt(2), s.codePointAt(3), s.codePointAt(4), s.codePointAt(5), s.codePointAt(6));
}
if (length == 8) {
return exports.$stringAllocate1_8(s.codePointAt(0), s.codePointAt(1), s.codePointAt(2), s.codePointAt(3), s.codePointAt(4), s.codePointAt(5), s.codePointAt(6), s.codePointAt(7));
}
if (length == 9) {
return exports.$stringAllocate1_9(s.codePointAt(0), s.codePointAt(1), s.codePointAt(2), s.codePointAt(3), s.codePointAt(4), s.codePointAt(5), s.codePointAt(6), s.codePointAt(7), s.codePointAt(8));
}
if (length == 10) {
return exports.$stringAllocate1_10(s.codePointAt(0), s.codePointAt(1), s.codePointAt(2), s.codePointAt(3), s.codePointAt(4), s.codePointAt(5), s.codePointAt(6), s.codePointAt(7), s.codePointAt(8), s.codePointAt(9));
}
}
const dartString = exports.$stringAllocate1(length);
const write = exports.$stringWrite1;
for (let i = 0; i < length; i++) {
write(dartString, i, s.codePointAt(i));
}
return dartString;
} else {
const dartString = exports.$stringAllocate2(length);
const write = exports.$stringWrite2;
for (let i = 0; i < length; i++) {
write(dartString, i, s.charCodeAt(i));
}
return dartString;
}
}
,
_247: l => new Array(l),
_251: (o, p) => o[p],
_255: o => String(o),
_260: x0 => x0.index,
_262: x0 => x0.length,
_264: (x0,x1) => x0[x1],
_265: (x0,x1) => x0.exec(x1),
_267: x0 => x0.flags,
_268: x0 => x0.multiline,
_269: x0 => x0.ignoreCase,
_270: x0 => x0.unicode,
_271: x0 => x0.dotAll,
_272: (x0,x1) => x0.lastIndex = x1
};
const baseImports = {
dart2wasm: dart2wasm,
Math: Math,
Date: Date,
Object: Object,
Array: Array,
Reflect: Reflect,
};
const jsStringPolyfill = {
"charCodeAt": (s, i) => s.charCodeAt(i),
"compare": (s1, s2) => {
if (s1 < s2) return -1;
if (s1 > s2) return 1;
return 0;
},
"concat": (s1, s2) => s1 + s2,
"equals": (s1, s2) => s1 === s2,
"fromCharCode": (i) => String.fromCharCode(i),
"length": (s) => s?.length||0,
"substring": (s, a, b) => s.substring(a, b),
};
dartInstance = await WebAssembly.instantiate(await modulePromise, {
...baseImports,
...(await importObjectPromise),
"wasm:js-string": jsStringPolyfill,
});
return dartInstance;
}
// Call the main function for the instantiated module
// `moduleInstance` is the instantiated dart2wasm module
// `args` are any arguments that should be passed into the main function.
export const invoke = (moduleInstance, ...args) => {
moduleInstance.exports.$invokeMain(args);
}
export let format;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,10 @@
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

@@ -0,0 +1,8 @@
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

@@ -0,0 +1,148 @@
/**
* Prettier Plugin for Dart formatting using dart_fmt WebAssembly
*
* This plugin provides support for formatting Dart files using the dart_fmt WASM implementation.
* dart_fmt is the official Dart code formatter integrated into the Dart SDK.
*/
import type { Plugin, Parser, Printer } from 'prettier';
// Import the Dart formatter WASM module
import dartInit, { format } from './dart_fmt_vite.js';
const parserName = 'dart';
// Language configuration
const languages = [
{
name: 'Dart',
aliases: ['dart'],
parsers: [parserName],
extensions: ['.dart'],
aceMode: 'dart',
tmScope: 'source.dart',
linguistLanguageId: 103,
vscodeLanguageIds: ['dart']
}
];
// Parser configuration
const dartParser: Parser<string> = {
astFormat: parserName,
parse: (text: string) => text,
locStart: () => 0,
locEnd: (node: string) => node.length,
};
// Initialize Dart WASM module
let initPromise: Promise<void> | null = null;
let isInitialized = false;
function initDart(): Promise<void> {
if (initPromise) {
return initPromise;
}
initPromise = (async () => {
if (!isInitialized) {
await dartInit();
isInitialized = true;
}
})();
return initPromise;
}
// Printer configuration
const dartPrinter: Printer<string> = {
print: (path, options) => {
try {
if (!isInitialized) {
console.warn('Dart WASM module not initialized, returning original text');
return (path as any).getValue ? (path as any).getValue() : path.node;
}
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);
return formatted.trim();
} catch (error) {
console.warn('Dart formatting failed:', error);
// Return original text if formatting fails
return (path as any).getValue ? (path as any).getValue() : path.node;
}
},
};
// 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 {
const config: any = {};
// Map Prettier options to Dart formatter config
if (options.printWidth !== undefined) {
config.line_width = options.printWidth;
}
if (options.endOfLine !== undefined) {
config.line_ending = options.endOfLine === 'crlf' ? 'crlf' : 'lf';
}
// Dart language version (if specified)
if (options.dartLanguageVersion !== undefined) {
config.language_version = options.dartLanguageVersion;
}
return config;
}
// Plugin options
const options = {
dartLanguageVersion: {
since: '0.0.1',
category: 'Format' as const,
type: 'string' as const,
default: undefined,
description: 'Dart language version (e.g., "3.0", "2.17")'
}
};
// Plugin object
const dartPlugin: Plugin = {
languages,
parsers: {
[parserName]: dartParser,
},
printers: {
[parserName]: dartPrinter,
},
options,
};
// Initialize WASM module when plugin loads
initDart().catch(error => {
console.warn('Failed to initialize Dart WASM module:', error);
});
export default dartPlugin;
export { languages };
export const parsers = dartPlugin.parsers;
export const printers = dartPlugin.printers;