diff --git a/types/prettier/index.d.ts b/types/prettier/index.d.ts new file mode 100644 index 0000000000..e02fd9a587 --- /dev/null +++ b/types/prettier/index.d.ts @@ -0,0 +1,103 @@ +// Type definitions for prettier 1.5 +// Project: https://github.com/prettier/prettier +// Definitions by: Ika +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +import { File } from 'babel-types'; + +export type AST = File; + +export type BuiltInParser = (text: string, options?: any) => AST; +export type BuiltInParserName = 'babylon' | 'flow' | 'typescript' | 'postcss' | 'json' | 'graphql'; + +export type CustomParser = (text: string, parsers: Record, options: Options) => AST; + +export interface Options { + /** + * Specify the line length that the printer will wrap on. + */ + printWidth?: number; + /** + * Specify the number of spaces per indentation-level. + */ + tabWidth?: number; + /** + * Indent lines with tabs instead of spaces + */ + useTabs?: boolean; + /** + * Print semicolons at the ends of statements. + */ + semi?: boolean; + /** + * Use single quotes instead of double quotes. + */ + singleQuote?: boolean; + /** + * Print trailing commas wherever possible. + */ + trailingComma?: 'none' | 'es5' | 'all'; + /** + * Print spaces between brackets in object literals. + */ + 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. + */ + jsxBracketSameLine?: boolean; + /** + * Format only a segment of a file. + */ + rangeStart?: number; + /** + * Format only a segment of a file. + */ + rangeEnd?: number; + /** + * Specify which parser to use. + */ + parser?: BuiltInParserName | CustomParser; + /** + * Specify the input filepath. This will be used to do parser inference. + */ + filepath?: string; +} + +export interface CursorOptions extends Options { + /** + * Specify where the cursor is. + */ + cursorOffset: number; + rangeStart?: never; + rangeEnd?: never; +} + +export interface CursorResult { + formatted: string; + cursorOffset: number; +} + +/** + * `format` is used to format text using Prettier. [Options](https://github.com/prettier/prettier#options) may be provided to override the defaults. + */ +export function format(source: string, options?: Options): string; + +/** + * `check` checks to see if the file has been formatted with Prettier given those options and returns a `Boolean`. + * This is similar to the `--list-different` parameter in the CLI and is useful for running Prettier in CI scenarios. + */ +export function check(source: string, options?: Options): boolean; + +/** + * `formatWithCursor` both formats the code, and translates a cursor position from unformatted code to formatted code. + * This is useful for editor integrations, to prevent the cursor from moving when code is formatted. + * + * The `cursorOffset` option should be provided, to specify where the cursor is. This option cannot be used with `rangeStart` and `rangeEnd`. + */ +export function formatWithCursor(source: string, options: CursorOptions): CursorResult; + +/** + * `version` field in `package.json` + */ +export const version: string; diff --git a/types/prettier/prettier-tests.ts b/types/prettier/prettier-tests.ts new file mode 100644 index 0000000000..d50cc47c98 --- /dev/null +++ b/types/prettier/prettier-tests.ts @@ -0,0 +1,19 @@ +import * as prettier from 'prettier'; +import { ExpressionStatement, CallExpression, Identifier } from 'babel-types'; + +const formatted = prettier.format("foo ( );", { semi: false }); + +const isFormatted = prettier.check("foo ( );", { semi: false }); + +const result = prettier.formatWithCursor(" 1", { cursorOffset: 2 }); + +const customFormatted = prettier.format("lodash ( )", { + parser(text, { babylon }) { + const ast = babylon(text); + const statement = ast.program.body[0] as ExpressionStatement; + const expression = statement.expression as CallExpression; + const identifier = expression.callee as Identifier; + identifier.name = "_"; + return ast; + } +}); diff --git a/types/prettier/tsconfig.json b/types/prettier/tsconfig.json new file mode 100644 index 0000000000..5a6c388e91 --- /dev/null +++ b/types/prettier/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "prettier-tests.ts" + ] +} diff --git a/types/prettier/tslint.json b/types/prettier/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/prettier/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}