diff --git a/CHANGELOG.md b/CHANGELOG.md index ac541cdd6..e8474dd1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to the "prettier-vscode" extension will be documented in thi Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +## [Unreleased] +- New setting `jsxBracketSameLine`. (prettier 0.17.0) +- Changed `trailingComma` setting `['none', 'es5', 'all']` (prettier 0.19.0) + ## [0.7.0] - Removed `Prettier` action. - Use vscode actions `Format Document` and `Format Selection`. diff --git a/README.md b/README.md index f02623bf4..3954332a8 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,18 @@ Use the flow parser instead of babylon. **Deprecated** use `parser: 'flow'` inst #### singleQuote (default: false) If true, will use single instead of double quotes -#### trailingComma (default: false) -Controls the printing of trailing commas wherever possible +#### trailingComma (default: 'none') +Controls the printing of trailing commas wherever possible. Valid options: + - "none" - No trailing commas + - "es5" - Trailing commas where valid in ES5 (objects, arrays, etc) + - "all" - Trailing commas wherever possible (function arguments) #### bracketSpacing (default: true) Controls the printing of spaces inside object literals +#### jsxBracketSameLine (default: false) +If true, puts the `>` of a multi-line jsx element at the end of the last line instead of being alone on the next line + #### parser (default: 'babylon') Which parser to use. Valid options are 'flow' and 'babylon' diff --git a/package.json b/package.json index a47c7a7c2..a6a07b9fe 100644 --- a/package.json +++ b/package.json @@ -55,14 +55,24 @@ "description": "If true, will use single instead of double quotes" }, "prettier.trailingComma": { - "type": "boolean", - "default": false, + "type": "string", + "enum": [ + "none", + "es5", + "all" + ], + "default": "none", "description": "Controls the printing of trailing commas wherever possible" }, "prettier.bracketSpacing": { "type": "boolean", "default": true, - "description": "Controls the printing of spaces inside array and objects" + "description": "Controls the printing of spaces inside object literals" + }, + "prettier.jsxBracketSameLine": { + "type": "boolean", + "default": false, + "description": "If true, puts the `>` of a multi-line jsx element at the end of the last line instead of being alone on the next line" }, "prettier.parser": { "type": "string", @@ -90,6 +100,6 @@ "@types/mocha": "^2.2.32" }, "dependencies": { - "prettier": "0.17.0" + "prettier": "0.21.0" } -} +} \ No newline at end of file diff --git a/src/PrettierEditProvider.ts b/src/PrettierEditProvider.ts index 2516cf7ed..4dff8be8b 100644 --- a/src/PrettierEditProvider.ts +++ b/src/PrettierEditProvider.ts @@ -11,14 +11,18 @@ import { const prettier = require('prettier'); +type ParserOption = 'babylon' | 'flow' +type TrailingCommaOption = 'none' | 'es5' | 'all' | boolean /* deprecated boolean*/ + interface PrettierConfig { printWidth: number, tabWidth: number, useFlowParser: boolean, // deprecated singleQuote: boolean, - trailingComma: boolean, + trailingComma: TrailingCommaOption, bracketSpacing: boolean, - parser: string + jsxBracketSameLine: boolean, + parser: ParserOption } function format(text: string): string { @@ -30,14 +34,24 @@ function format(text: string): string { if (!parser) { // unset config parser = config.useFlowParser ? 'flow' : 'babylon'; } + /* + handle trailingComma changes boolean -> string + */ + let trailingComma = config.trailingComma; + if (trailingComma === true) { + trailingComma = 'es5'; + } else if (trailingComma === false) { + trailingComma = 'none'; + } let transformed: string; try { return prettier.format(text, { printWidth: config.printWidth, tabWidth: config.tabWidth, singleQuote: config.singleQuote, - trailingComma: config.trailingComma, + trailingComma, bracketSpacing: config.bracketSpacing, + jsxBracketSameLine: config.jsxBracketSameLine, parser: parser }); } catch (e) { @@ -72,4 +86,4 @@ class PrettierEditProvider implements } export default PrettierEditProvider; -export { PrettierConfig } \ No newline at end of file +export { PrettierConfig } diff --git a/src/extension.ts b/src/extension.ts index 22b0ded29..cb6657878 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -9,12 +9,19 @@ import EditProvider, { PrettierConfig } from './PrettierEditProvider'; const VALID_LANG: DocumentSelector = ['javascript', 'javascriptreact']; -export function activate(context: ExtensionContext) { +function checkConfig() { const config: PrettierConfig = workspace.getConfiguration('prettier') as any; - const editProvider = new EditProvider() if (config.useFlowParser) { - window.showWarningMessage("Option 'useFlowParser' has been deprecated. Use 'parser: \"flow\"' instead.") + window.showWarningMessage("Option 'useFlowParser' has been deprecated. Use 'parser: \"flow\"' instead."); + } + if (typeof config.trailingComma === 'boolean') { + window.showWarningMessage("Option 'trailingComma' as a boolean value has been deprecated. Use 'none', 'es5' or 'all' instead."); } +} +export function activate(context: ExtensionContext) { + const editProvider = new EditProvider(); + checkConfig(); + context.subscriptions.push( languages.registerDocumentRangeFormattingEditProvider(VALID_LANG, editProvider) );