Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion apps/oxfmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ oxc_allocator = { workspace = true, features = ["pool"] }
oxc_diagnostics = { workspace = true }
oxc_formatter = { workspace = true }
oxc_language_server = { workspace = true, default-features = false, features = ["formatter"] }
oxc_napi = { workspace = true, optional = true }
oxc_parser = { workspace = true }
oxc_span = { workspace = true }

Expand Down Expand Up @@ -66,6 +67,6 @@ mimalloc-safe = { workspace = true, optional = true, features = ["skip_collect_o

[features]
default = ["napi"]
napi = ["dep:napi", "dep:napi-derive"]
napi = ["dep:napi", "dep:napi-derive", "dep:oxc_napi", "oxc_formatter/napi"]
allocator = ["dep:mimalloc-safe"]
detect_code_removal = ["oxc_formatter/detect_code_removal"]
207 changes: 206 additions & 1 deletion apps/oxfmt/src-js/bindings.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,208 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export declare const enum ArrowParentheses {
Always = 0,
AsNeeded = 1
}

export declare const enum AttributePosition {
Auto = 0,
Multiline = 1
}

/** Put the `>` of a multi-line HTML or JSX element at the end of the last line instead of being alone on the next line (does not apply to self closing elements). */
export type BracketSameLine =
boolean

export type BracketSpacing =
boolean

export declare const enum EmbeddedLanguageFormatting {
/** Enable formatting for embedded languages. */
Auto = 0,
/** Disable formatting for embedded languages. */
Off = 1
}

export declare const enum Expand {
/**
* Objects are expanded when the first property has a leading newline. Arrays are always
* expanded if they are shorter than the line width.
*/
Auto = 0,
/** Objects and arrays are always expanded. */
Always = 1,
/** Objects and arrays are never expanded, if they are shorter than the line width. */
Never = 2
}

export interface FormatOptions {
/** The indent style. */
indentStyle: IndentStyle
/** The indent width. */
indentWidth: IndentWidth
/** The type of line ending. */
lineEnding: LineEnding
/** What's the max width of a line. Defaults to 100. */
lineWidth: LineWidth
/** The style for quotes. Defaults to double. */
quoteStyle: QuoteStyle
/** The style for JSX quotes. Defaults to double. */
jsxQuoteStyle: QuoteStyle
/** When properties in objects are quoted. Defaults to as-needed. */
quoteProperties: QuoteProperties
/** Print trailing commas wherever possible in multi-line comma-separated syntactic structures. Defaults to "all". */
trailingCommas: TrailingCommas
/** Whether the formatter prints semicolons for all statements, class members, and type members or only when necessary because of [ASI](https://tc39.es/ecma262/multipage/ecmascript-language-lexical-grammar.html#sec-automatic-semicolon-insertion). */
semicolons: Semicolons
/** Whether to add non-necessary parentheses to arrow functions. Defaults to "always". */
arrowParentheses: ArrowParentheses
/** Whether to insert spaces around brackets in object literals. Defaults to true. */
bracketSpacing: BracketSpacing
/** Whether to hug the closing bracket of multiline HTML/JSX tags to the end of the last line, rather than being alone on the following line. Defaults to false. */
bracketSameLine: BracketSameLine
/** Attribute position style. By default auto. */
attributePosition: AttributePosition
/** Whether to expand object and array literals to multiple lines. Defaults to "auto". */
expand: Expand
/**
* Controls the position of operators in binary expressions. [**NOT SUPPORTED YET**]
*
* Accepted values are:
* - `"start"`: Places the operator at the beginning of the next line.
* - `"end"`: Places the operator at the end of the current line (default).
*/
experimentalOperatorPosition: OperatorPosition
/**
* Try prettier's new ternary formatting before it becomes the default behavior. [**NOT SUPPORTED YET**]
*
* Valid options:
* - `true` - Use curious ternaries, with the question mark after the condition.
* - `false` - Retain the default behavior of ternaries; keep question marks on the same line as the consequent.
*/
experimentalTernaries: boolean
/** Enable formatting for embedded languages (e.g., CSS, SQL, GraphQL) within template literals. Defaults to "auto". */
embeddedLanguageFormatting: EmbeddedLanguageFormatting
/** Sort import statements. By default disabled. */
experimentalSortImports?: SortImportsOptions
}

export declare const enum IndentStyle {
/** Tab */
Tab = 0,
/** Space */
Space = 1
}

export type IndentWidth =
number

export declare const enum LineEnding {
/** Line Feed only (
), common on Linux and macOS as well as inside git repos */
Lf = 0,
/** Carriage Return + Line Feed characters (\r
), common on Windows */
Crlf = 1,
/** Carriage Return character only (\r), used very rarely */
Cr = 2
}

/**
* Validated value for the `line_width` formatter options
*
* The allowed range of values is 1..=320
*/
export type LineWidth =
number

export declare const enum OperatorPosition {
/** When binary expressions wrap lines, print operators at the start of new lines. */
Start = 0,
End = 1
}

export declare const enum QuoteProperties {
/** Only add quotes around object properties where required. */
AsNeeded = 0,
/** Respect the input use of quotes in object properties. */
Preserve = 1,
/** If at least one property in an object requires quotes, quote all properties. [**NOT SUPPORTED YET**] */
Consistent = 2
}

export declare const enum QuoteStyle {
Double = 0,
Single = 1
}

export declare const enum Semicolons {
Always = 0,
AsNeeded = 1
}

export interface SortImportsOptions {
/**
* Partition imports by newlines.
* Default is `false`.
*/
partitionByNewline: boolean
/**
* Partition imports by comments.
* Default is `false`.
*/
partitionByComment: boolean
/**
* Sort side effects imports.
* Default is `false`.
*/
sortSideEffects: boolean
/**
* Sort order (asc or desc).
* Default is ascending (asc).
*/
order: SortOrder
/**
* Ignore case when sorting.
* Default is `true`.
*/
ignoreCase: boolean
/**
* Whether to insert blank lines between different import groups.
* - `true`: Insert one blank line between groups (default)
* - `false`: No blank lines between groups
*
* NOTE: Cannot be used together with `partition_by_newline: true`.
*/
newlinesBetween: boolean
/**
* Prefixes for internal imports.
* Defaults to `["~/", "@/"]`.
*/
internalPattern: Array<string>
/**
* Groups configuration for organizing imports.
* Each inner `Vec` represents a group, and multiple group names in the same `Vec` are treated as one.
*/
groups: Array<Array<string>>
}

export declare const enum SortOrder {
/** Sort in ascending order (A-Z). */
Asc = 0,
/** Sort in descending order (Z-A). */
Desc = 1
}

/** Print trailing commas wherever possible in multi-line comma-separated syntactic structures. */
export declare const enum TrailingCommas {
/** Trailing commas wherever possible (including function parameters and calls). */
All = 0,
/** Trailing commas where valid in ES5 (objects, arrays, etc.). No trailing commas in type parameters in TypeScript. */
Es5 = 1,
/** No trailing commas. */
None = 2
}
/**
* NAPI entry point.
*
Expand All @@ -11,4 +214,6 @@
*
* Returns `true` if formatting succeeded without errors, `false` otherwise.
*/
export declare function format(args: Array<string>, setupConfigCb: (configJSON: string, numThreads: number) => Promise<string[]>, formatEmbeddedCb: (tagName: string, code: string) => Promise<string>, formatFileCb: (parserName: string, fileName: string, code: string) => Promise<string>): Promise<boolean>
export declare function formatInternal(args: Array<string>, setupConfigCb: (configJSON: string, numThreads: number) => Promise<string[]>, formatEmbeddedCb: (tagName: string, code: string) => Promise<string>, formatFileCb: (parserName: string, fileName: string, code: string) => Promise<string>): Promise<boolean>

export declare function formatSync(source: string, options: FormatOptions): string
17 changes: 15 additions & 2 deletions apps/oxfmt/src-js/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,5 +575,18 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { format } = nativeBinding
export { format }
const { ArrowParentheses, AttributePosition, EmbeddedLanguageFormatting, Expand, IndentStyle, LineEnding, OperatorPosition, QuoteProperties, QuoteStyle, Semicolons, SortOrder, TrailingCommas, formatInternal, formatSync } = nativeBinding
export { ArrowParentheses }
export { AttributePosition }
export { EmbeddedLanguageFormatting }
export { Expand }
export { IndentStyle }
export { LineEnding }
export { OperatorPosition }
export { QuoteProperties }
export { QuoteStyle }
export { Semicolons }
export { SortOrder }
export { TrailingCommas }
export { formatInternal }
export { formatSync }
4 changes: 2 additions & 2 deletions apps/oxfmt/src-js/cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { format } from "./bindings.js";
import { formatInternal } from "./bindings.js";
import { setupConfig, formatEmbeddedCode, formatFile } from "./prettier-proxy.js";

const args = process.argv.slice(2);

// Call the Rust formatter with our JS callback
const success = await format(args, setupConfig, formatEmbeddedCode, formatFile);
const success = await formatInternal(args, setupConfig, formatEmbeddedCode, formatFile);

// NOTE: It's recommended to set `process.exitCode` instead of calling `process.exit()`.
// `process.exit()` kills the process immediately and `stdout` may not be flushed before process dies.
Expand Down
Loading