Skip to content

Commit

Permalink
Merge pull request #38 from CiGit/settings-update
Browse files Browse the repository at this point in the history
Update to prettier 0.21.0
  • Loading branch information
esbenp authored Mar 6, 2017
2 parents d039ce4 + fa6e980 commit 532be9c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -90,6 +100,6 @@
"@types/mocha": "^2.2.32"
},
"dependencies": {
"prettier": "0.17.0"
"prettier": "0.21.0"
}
}
}
22 changes: 18 additions & 4 deletions src/PrettierEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -72,4 +86,4 @@ class PrettierEditProvider implements
}

export default PrettierEditProvider;
export { PrettierConfig }
export { PrettierConfig }
13 changes: 10 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down

0 comments on commit 532be9c

Please sign in to comment.