-
-
Notifications
You must be signed in to change notification settings - Fork 843
feat(oxfmt): Expose Node.js API: format(fileName, sourceText, options?)
#16939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
graphite-app
merged 1 commit into
main
from
12-16-feat_oxfmt_expose_node.js_api_format_
Dec 16, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,113 @@ | ||
| export * from "./bindings.js"; | ||
| export { setupConfig, formatEmbeddedCode, formatFile } from "./prettier-proxy.js"; | ||
| import { format as napiFormat } from "./bindings.js"; | ||
| import { setupConfig, formatEmbeddedCode, formatFile } from "./prettier-proxy.js"; | ||
|
|
||
| export async function format(fileName: string, sourceText: string, options?: FormatOptions) { | ||
| if (typeof fileName !== "string") throw new TypeError("`fileName` must be a string"); | ||
| if (typeof sourceText !== "string") throw new TypeError("`sourceText` must be a string"); | ||
|
|
||
| return napiFormat( | ||
| fileName, | ||
| sourceText, | ||
| options ?? {}, | ||
| setupConfig, | ||
| formatEmbeddedCode, | ||
| formatFile, | ||
Dunqing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| } | ||
|
|
||
| // NOTE: Regarding the handwritten TypeScript types. | ||
| // | ||
| // Initially, I tried to use the `Oxfmtrc` struct to automatically generate types with `napi(object)`, | ||
| // but since `Oxfmtrc` has many fields defined as `enum`, the API usage would look like this: | ||
| // ```ts | ||
| // oxfmt.format("file.ts", "const a=1;", { | ||
| // endOfLine: oxfmt.EndOfLine.Lf, | ||
| // // ... | ||
| // }); | ||
| // ``` | ||
| // Since it cannot be specified with string literals, the API usability is not good. | ||
| // | ||
| // Also, since `Oxfmtrc` is primarily a configuration file, | ||
| // it includes fields like `ignorePatterns` that are unnecessary for the API. | ||
| // | ||
| // Therefore, I decided that if I were to create a dedicated struct for `napi(object)`, | ||
| // it would be better to just handwrite the TypeScript types. | ||
| // | ||
| // There is a mechanism to generate JSON Schema, so it might be possible to generate type definitions from that in the future. | ||
|
|
||
| /** | ||
| * Configuration options for the Oxfmt. | ||
| * | ||
| * Most options are the same as Prettier's options. | ||
| * See also <https://prettier.io/docs/options> | ||
| * | ||
| * In addition, some options are our own extensions. | ||
| */ | ||
| export type FormatOptions = { | ||
| /** Use tabs for indentation or spaces. (Default: `false`) */ | ||
| useTabs?: boolean; | ||
| /** Number of spaces per indentation level. (Default: `2`) */ | ||
| tabWidth?: number; | ||
| /** Which end of line characters to apply. (Default: `"lf"`) */ | ||
| endOfLine?: "lf" | "crlf" | "cr"; | ||
| /** The line length that the printer will wrap on. (Default: `100`) */ | ||
| printWidth?: number; | ||
| /** Use single quotes instead of double quotes. (Default: `false`) */ | ||
| singleQuote?: boolean; | ||
| /** Use single quotes instead of double quotes in JSX. (Default: `false`) */ | ||
| jsxSingleQuote?: boolean; | ||
| /** Change when properties in objects are quoted. (Default: `"as-needed"`) */ | ||
| quoteProps?: "as-needed" | "consistent" | "preserve"; | ||
| /** Print trailing commas wherever possible. (Default: `"all"`) */ | ||
| trailingComma?: "all" | "es5" | "none"; | ||
| /** Print semicolons at the ends of statements. (Default: `true`) */ | ||
| semi?: boolean; | ||
| /** Include parentheses around a sole arrow function parameter. (Default: `"always"`) */ | ||
| arrowParens?: "always" | "avoid"; | ||
| /** Print spaces between brackets in object literals. (Default: `true`) */ | ||
| bracketSpacing?: boolean; | ||
| /** | ||
| * Put the `>` of a multi-line JSX element at the end of the last line | ||
| * instead of being alone on the next line. (Default: `false`) | ||
| */ | ||
| bracketSameLine?: boolean; | ||
| /** | ||
| * How to wrap object literals when they could fit on one line or span multiple lines. (Default: `"preserve"`) | ||
| * NOTE: In addition to Prettier's `"preserve"` and `"collapse"`, we also support `"always"`. | ||
| */ | ||
| objectWrap?: "preserve" | "collapse" | "always"; | ||
| /** Put each attribute on a new line in JSX. (Default: `false`) */ | ||
| singleAttributePerLine?: boolean; | ||
| /** Control whether formats quoted code embedded in the file. (Default: `"auto"`) */ | ||
| embeddedLanguageFormatting?: "auto" | "off"; | ||
| /** Experimental: Sort import statements. Disabled by default. */ | ||
| experimentalSortImports?: SortImportsOptions; | ||
| /** Experimental: Sort `package.json` keys. (Default: `true`) */ | ||
| experimentalSortPackageJson?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Configuration options for sort imports. | ||
| */ | ||
| export type SortImportsOptions = { | ||
| /** Partition imports by newlines. (Default: `false`) */ | ||
| partitionByNewline?: boolean; | ||
| /** Partition imports by comments. (Default: `false`) */ | ||
| partitionByComment?: boolean; | ||
| /** Sort side-effect imports. (Default: `false`) */ | ||
| sortSideEffects?: boolean; | ||
| /** Sort order. (Default: `"asc"`) */ | ||
| order?: "asc" | "desc"; | ||
| /** Ignore case when sorting. (Default: `true`) */ | ||
| ignoreCase?: boolean; | ||
| /** Add newlines between import groups. (Default: `true`) */ | ||
| newlinesBetween?: boolean; | ||
| /** Glob patterns to identify internal imports. */ | ||
| internalPattern?: string[]; | ||
| /** | ||
| * Custom groups configuration for organizing imports. | ||
| * Each array element represents a group, and multiple group names in the same array are treated as one. | ||
| * Accepts both `string` and `string[]` as group elements. | ||
| */ | ||
| groups?: (string | string[])[]; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { format } from "../dist/index.js"; | ||
| import { describe, expect, test } from "vitest"; | ||
| import type { FormatOptions } from "../dist/index.js"; | ||
|
|
||
| describe("API Tests", () => { | ||
| test("`format()` function exists", () => { | ||
| expect(typeof format).toBe("function"); | ||
| }); | ||
|
|
||
| test("should `format()` multiple times", async () => { | ||
| const result1 = await format("a.ts", "const x:number=42"); | ||
| expect(result1.code).toBe("const x: number = 42;\n"); | ||
| expect(result1.errors).toStrictEqual([]); | ||
|
|
||
| const result2 = await format("a.json", '{"key": "value"}'); | ||
| expect(result2.code).toBe('{ "key": "value" }\n'); | ||
| expect(result2.errors).toStrictEqual([]); | ||
| }); | ||
|
|
||
| test("should TS types and options work", async () => { | ||
| const options: FormatOptions = { | ||
| quoteProps: "as-needed", // Can be string literal | ||
| printWidth: 120, | ||
| semi: false, | ||
| experimentalSortPackageJson: false, | ||
| experimentalSortImports: { | ||
| // Can be optional object | ||
| partitionByComment: false, | ||
| }, | ||
| }; | ||
|
|
||
| const result = await format("a.ts", "const x={'y':1}", options); | ||
| expect(result.code).toBe("const x = { y: 1 }\n"); | ||
| expect(result.errors).toStrictEqual([]); | ||
|
|
||
| const { errors } = await format("a.ts", "const x={'y':1}", { | ||
| // @ts-expect-error: Test invalid options is validated | ||
| semi: "invalid", | ||
| }); | ||
| expect(errors.length).toBe(1); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.