export interface Options { /** * Number of spaces the pretty-printer should use per tab for * indentation. If you do not pass this option explicitly, it will be * (quite reliably!) inferred from the original code. * @default 4 */ tabWidth?: number; /** * If you really want the pretty-printer to use tabs instead of spaces, * make this option true. * @default false */ useTabs?: boolean; /** * The reprinting code leaves leading whitespace untouched unless it has * to reindent a line, or you pass false for this option. * @default true */ reuseWhitespace?: boolean; /** * Override this option to use a different line terminator, e.g. \r\n. * @default require("os").EOL || "\n" */ lineTerminator?: string; /** * Some of the pretty-printer code (such as that for printing function * parameter lists) makes a valiant attempt to prevent really long * lines. You can adjust the limit by changing this option; however, * there is no guarantee that line length will fit inside this limit. * @default 74 */ wrapColumn?: number; /** * If you want esprima not to throw exceptions when it encounters * non-fatal errors, keep this option true. * @default true */ tolerant?: boolean; /** * If you want to override the quotes used in string literals, specify * either "single", "double", or "auto" here ("auto" will select the one * which results in the shorter literal) Otherwise, use double quotes. * @default null */ quote?: "single" | "double" | "auto" | null; /** * Controls the printing of trailing commas in object literals, array * expressions and function parameters. * * This option could either be: * * Boolean - enable/disable in all contexts (objects, arrays and function params). * * Object - enable/disable per context. * * Example: * trailingComma: { * objects: true, * arrays: true, * parameters: false, * } * * @default false */ trailingComma?: boolean | { objects?: boolean, arrays?: boolean, parameters?: boolean, }; /** * Controls the printing of spaces inside array brackets. * See: http://eslint.org/docs/rules/array-bracket-spacing * @default false */ arrayBracketSpacing?: boolean; /** * Controls the printing of spaces inside object literals, * destructuring assignments, and import/export specifiers. * See: http://eslint.org/docs/rules/object-curly-spacing * @default true */ objectCurlySpacing?: boolean; /** * If you want parenthesis to wrap single-argument arrow function * parameter lists, pass true for this option. * @default false */ arrowParensAlways?: boolean; /** * There are 2 supported syntaxes (`,` and `;`) in Flow Object Types; * The use of commas is in line with the more popular style and matches * how objects are defined in JS, making it a bit more natural to write. * @default true */ flowObjectCommas?: boolean; /** * Whether to return an array of .tokens on the root AST node. * @default true */ tokens?: boolean; } const defaults: Options = { tabWidth: 4, useTabs: false, reuseWhitespace: true, lineTerminator: require("os").EOL || "\n", wrapColumn: 74, // Aspirational for now. tolerant: true, quote: null, trailingComma: false, arrayBracketSpacing: false, objectCurlySpacing: true, arrowParensAlways: false, flowObjectCommas: true, tokens: true } const hasOwn = defaults.hasOwnProperty export type NormalizedOptions = Required // Copy options and fill in default values. export function normalize(opts?: Options): NormalizedOptions { const options = opts || defaults function get(key: keyof Options): any { return hasOwn.call(options, key) ? options[key] : defaults[key] } return { tabWidth: +get("tabWidth"), useTabs: !!get("useTabs"), reuseWhitespace: !!get("reuseWhitespace"), lineTerminator: get("lineTerminator"), wrapColumn: Math.max(+get("wrapColumn"), 0), tolerant: !!get("tolerant"), quote: get("quote"), trailingComma: get("trailingComma"), arrayBracketSpacing: get("arrayBracketSpacing"), objectCurlySpacing: get("objectCurlySpacing"), arrowParensAlways: get("arrowParensAlways"), flowObjectCommas: get("flowObjectCommas"), tokens: !!get("tokens") } }