Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Prettier require resolution #39

Merged
merged 2 commits into from
Mar 17, 2017
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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]
- Resolve 'prettier' against formatted file. Nearest upward *node_modules/prettier*

## [0.10.0]
- New setting `jsxBracketSameLine`. (prettier 0.17.0)
Expand Down
36 changes: 23 additions & 13 deletions src/PrettierEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,29 @@ import {
Selection,
Position
} from 'vscode';

const prettier = require('prettier');
import requirePrettier from './requirePrettier';

type ParserOption = 'babylon' | 'flow'
type TrailingCommaOption = 'none' | 'es5' | 'all' | boolean /* deprecated boolean*/
type ShowAction = "Show";
interface PrettierConfig {
printWidth: number,
tabWidth: number,
useFlowParser: boolean, // deprecated
singleQuote: boolean,
trailingComma: TrailingCommaOption,
bracketSpacing: boolean,
jsxBracketSameLine: boolean,
parser: ParserOption
printWidth: number;
tabWidth: number;
useFlowParser: boolean; // deprecated
singleQuote: boolean;
trailingComma: TrailingCommaOption;
bracketSpacing: boolean;
jsxBracketSameLine: boolean;
parser: ParserOption;
}

/**
* Format the given text with prettier with user's configuration.
* @param text Text to format
* @param path formatting file's path
* @returns {string} formatted text
*/
function format(text: string): string {
function format(text: string, path: string): string {
const config: PrettierConfig = workspace.getConfiguration('prettier') as any;
/*
handle deprecated parser option
Expand All @@ -49,6 +51,8 @@ function format(text: string): string {
} else if (trailingComma === false) {
trailingComma = 'none';
}
const prettier = requirePrettier(path);

return prettier.format(text, {
printWidth: config.printWidth,
tabWidth: config.tabWidth,
Expand All @@ -75,7 +79,10 @@ class PrettierEditProvider implements
token: CancellationToken
): TextEdit[] {
try {
return [TextEdit.replace(range, format(document.getText(range)))];
return [TextEdit.replace(
range,
format(document.getText(range), document.fileName)
)];
} catch (e) {
let errorPosition
if (e.loc) {
Expand All @@ -94,7 +101,10 @@ class PrettierEditProvider implements
token: CancellationToken
): TextEdit[] {
try {
return [TextEdit.replace(fullDocumentRange(document), format(document.getText()))];
return [TextEdit.replace(
fullDocumentRange(document),
format(document.getText(), document.fileName)
)];
} catch (e) {
let errorPosition;
if (e.loc) {
Expand Down
25 changes: 25 additions & 0 deletions src/requirePrettier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('path');
const Module = require('module');

interface Prettier {
format: (string, PrettierConfig?) => string;
readonly version: string;
}

/**
* Require 'prettier' relative to given path.
* Fallback to packaged one if no prettier was found bottom up.
* @param {string} fspath file system path starting point to resolve 'prettier'
* @returns {Prettier} prettier
*/
function requirePrettier(fspath: string): Prettier {
const fileModule = new Module(fspath);
fileModule.paths = Module._nodeModulePaths(path.join(fspath, '..'));
try {
return fileModule.require('prettier');
} catch (e) {
// No local prettier found
}
return require('prettier');
}
export default requirePrettier;