eslint-plugin-format
Version:
Format various languages with formatters in ESLint
167 lines (161 loc) • 3.89 kB
JavaScript
import * as parserPlain from "eslint-parser-plain";
import { join } from "node:path";
import { messages, reportDifferences } from "eslint-formatting-reporter";
import { createSyncFn } from "synckit";
import { fileURLToPath } from "node:url";
//#region src/dir.ts
const dirWorkers = fileURLToPath(new URL("../workers", import.meta.url));
//#endregion
//#region src/rules/dprint.ts
let format$2;
var dprint_default = {
meta: {
type: "layout",
docs: { description: "Use dprint to format code" },
fixable: "whitespace",
schema: [{
type: "object",
properties: {
language: { type: "string" },
languageOptions: { type: "object" },
plugins: { type: "array" }
},
additionalProperties: true
}],
messages
},
create(context) {
if (!format$2) format$2 = createSyncFn(join(dirWorkers, "dprint.cjs"));
return { [context.sourceCode.ast.type || "Program"]() {
const sourceCode = context.sourceCode.text;
try {
reportDifferences(context, sourceCode, format$2(sourceCode, context.filename, context.options[0] || {}));
} catch (error) {
console.error(error);
context.report({
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 0
}
},
message: `Failed to format the code: ${error}`
});
}
} };
}
};
//#endregion
//#region src/rules/oxfmt.ts
let format$1;
var oxfmt_default = {
meta: {
type: "layout",
docs: { description: "Use oxfmt to format code" },
fixable: "whitespace",
schema: [{
type: "object",
additionalProperties: true
}],
messages
},
create(context) {
if (!format$1) format$1 = createSyncFn(join(dirWorkers, "oxfmt.cjs"));
return { [context.sourceCode.ast.type || "Program"]() {
const sourceCode = context.sourceCode.text;
try {
const { code: formatted, errors } = format$1(context.filename, sourceCode, context.options[0] || {});
if (errors.length) {
const error = errors[0];
if (error.message.startsWith("Unsupported file type:")) return;
else throw new Error(error.message);
}
reportDifferences(context, sourceCode, formatted);
} catch (error) {
context.report({
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 0
}
},
message: `Failed to format the code: ${error}`
});
}
} };
}
};
//#endregion
//#region src/rules/prettier.ts
let format;
var prettier_default = {
meta: {
type: "layout",
docs: { description: "Use Prettier to format code" },
fixable: "whitespace",
schema: [{
type: "object",
properties: { parser: { type: "string" } },
required: ["parser"],
additionalProperties: true
}],
messages
},
create(context) {
if (!format) format = createSyncFn(join(dirWorkers, "prettier.cjs"));
return { [context.sourceCode.ast.type || "Program"]() {
const sourceCode = context.sourceCode.text;
try {
reportDifferences(context, sourceCode, format(sourceCode, {
filepath: context.filename,
...context.options[0] || {}
}));
} catch (err) {
if (!(err instanceof SyntaxError)) {
context.report({
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 0
}
},
message: "Failed to format the code"
});
return;
}
let message = `Parsing error: ${err.message}`;
const error = err;
if (error.codeFrame) message = message.replace(`\n${error.codeFrame}`, "");
if (error.loc) message = message.replace(/ \(\d+:\d+\)$/, "");
context.report({
message,
loc: error.loc
});
}
} };
}
};
//#endregion
//#region src/index.ts
var src_default = {
parserPlain,
rules: {
prettier: prettier_default,
dprint: dprint_default,
oxfmt: oxfmt_default
}
};
//#endregion
export { src_default as default };