[prettier] Update to v2 (#43576)

* Remove babylon parser

* Remove postcss

* Add babel-ts parser

* Fix options

* Remove version parameter from getSupportInfo

* Fix util function types

* Fix version

* Fix tests

* Run prettier
This commit is contained in:
Sosuke Suzuki 2020-04-03 01:47:41 +09:00 committed by GitHub
parent 445f92ce91
commit 52100395d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 36 deletions

View File

@ -1,4 +1,4 @@
// Type definitions for prettier 1.19
// Type definitions for prettier 2.0
// Project: https://github.com/prettier/prettier, https://prettier.io
// Definitions by: Ika <https://github.com/ikatyang>,
// Ifiok Jr. <https://github.com/ifiokjr>,
@ -24,12 +24,11 @@ export interface FastPath<T = any> {
export type BuiltInParser = (text: string, options?: any) => AST;
export type BuiltInParserName =
| 'babylon' // deprecated
| 'babel'
| 'babel-flow'
| 'babel-ts'
| 'flow'
| 'typescript'
| 'postcss' // deprecated
| 'css'
| 'less'
| 'scss'
@ -66,7 +65,7 @@ export interface RequiredOptions extends doc.printer.Options {
jsxSingleQuote: boolean;
/**
* Print trailing commas wherever possible.
* @default 'none'
* @default 'es5'
*/
trailingComma: 'none' | 'es5' | 'all';
/**
@ -116,14 +115,10 @@ export interface RequiredOptions extends doc.printer.Options {
* In some cases you may want to rely on editor/viewer soft wrapping instead, so this option allows you to opt out.
* @default 'preserve'
*/
proseWrap:
| boolean // deprecated
| 'always'
| 'never'
| 'preserve';
proseWrap: 'always' | 'never' | 'preserve';
/**
* Include parentheses around a sole arrow function parameter.
* @default 'avoid'
* @default 'always'
*/
arrowParens: 'avoid' | 'always';
/**
@ -137,7 +132,7 @@ export interface RequiredOptions extends doc.printer.Options {
htmlWhitespaceSensitivity: 'css' | 'strict' | 'ignore';
/**
* Which end of line characters to apply.
* @default 'auto'
* @default 'lf'
*/
endOfLine: 'auto' | 'lf' | 'crlf' | 'cr';
/**
@ -176,11 +171,7 @@ export interface Parser {
}
export interface Printer {
print(
path: FastPath,
options: ParserOptions,
print: (path: FastPath) => Doc,
): Doc;
print(path: FastPath, options: ParserOptions, print: (path: FastPath) => Doc): Doc;
embed?: (
path: FastPath,
print: (path: FastPath) => Doc,
@ -200,8 +191,20 @@ export interface Printer {
printComments?: (path: FastPath, print: (path: FastPath) => Doc, options: ParserOptions, needsSemi: boolean) => Doc;
handleComments?: {
ownLine?: (commentNode: any, text: string, options: ParserOptions, ast: any, isLastComment: boolean) => boolean;
endOfLine?: (commentNode: any, text: string, options: ParserOptions, ast: any, isLastComment: boolean) => boolean;
remaining?: (commentNode: any, text: string, options: ParserOptions, ast: any, isLastComment: boolean) => boolean;
endOfLine?: (
commentNode: any,
text: string,
options: ParserOptions,
ast: any,
isLastComment: boolean,
) => boolean;
remaining?: (
commentNode: any,
text: string,
options: ParserOptions,
ast: any,
isLastComment: boolean,
) => boolean;
};
}
@ -379,11 +382,9 @@ export namespace getFileInfo {
}
/**
* Returns an object representing the parsers, languages and file types Prettier supports.
* If `version` is provided (e.g. `"1.5.0"`), information for that version will be returned,
* otherwise information for the current version will be returned.
* Returns an object representing the parsers, languages and file types Prettier supports for the current version.
*/
export function getSupportInfo(version?: string): SupportInfo;
export function getSupportInfo(): SupportInfo;
/**
* `version` field in `package.json`
@ -392,8 +393,9 @@ export const version: string;
// https://github.com/prettier/prettier/blob/master/src/common/util-shared.js
export namespace util {
function isNextLineEmpty(text: string, node: any, options: ParserOptions): boolean;
function isNextLineEmpty(text: string, node: any, locEnd: (node: any) => number): boolean;
function isNextLineEmptyAfterIndex(text: string, index: number): boolean;
function isPreviousLineEmpty(text: string, node: any, locStart: (node: any) => number): boolean;
function getNextNonSpaceNonCommentCharacterIndex(text: string, node: any, options: ParserOptions): number;
function makeString(rawContent: string, enclosingQuote: "'" | '"', unescapeUnnecessaryEscapes: boolean): string;
function addLeadingComment(node: any, commentNode: any): void;
@ -495,7 +497,10 @@ export namespace doc {
function printDocToDebug(doc: Doc): string;
}
namespace printer {
function printDocToString(doc: Doc, options: Options): {
function printDocToString(
doc: Doc,
options: Options,
): {
formatted: string;
cursorNodeStart?: number;
cursorNodeText?: string;
@ -522,7 +527,12 @@ export namespace doc {
function isEmpty(doc: Doc): boolean;
function isLineNext(doc: Doc): boolean;
function willBreak(doc: Doc): boolean;
function traverseDoc(doc: Doc, onEnter?: (doc: Doc) => void | boolean, onExit?: (doc: Doc) => void, shouldTraverseConditionalGroups?: boolean): void;
function traverseDoc(
doc: Doc,
onEnter?: (doc: Doc) => void | boolean,
onExit?: (doc: Doc) => void,
shouldTraverseConditionalGroups?: boolean,
): void;
function mapDoc<T>(doc: Doc, callback: (doc: Doc) => T): T;
function propagateBreaks(doc: Doc): void;
function removeLines(doc: Doc): Doc;

View File

@ -3,7 +3,7 @@ import { ExpressionStatement, CallExpression, Identifier } from 'babel-types';
import * as prettierStandalone from 'prettier/standalone';
import typescriptParser = require('prettier/parser-typescript');
import graphqlParser = require('prettier/parser-graphql');
import babylonParser = require('prettier/parser-babylon');
import babelParser = require('prettier/parser-babel');
import htmlParser = require('prettier/parser-html');
import markdownParser = require('prettier/parser-markdown');
import postcssParser = require('prettier/parser-postcss');
@ -16,8 +16,8 @@ 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);
parser(text, { babel }) {
const ast = babel(text);
const statement = ast.program.body[0] as ExpressionStatement;
const expression = statement.expression as CallExpression;
const identifier = expression.callee as Identifier;
@ -67,7 +67,6 @@ const configFilePathInSpecificPath = prettier.resolveConfigFile.sync('/path');
prettier.clearConfigCache();
const currentSupportInfo = prettier.getSupportInfo();
const specificSupportInfo = prettier.getSupportInfo('1.8.0');
prettierStandalone.formatWithCursor(' 1', { cursorOffset: 2, parser: 'babel' });
@ -76,10 +75,12 @@ prettierStandalone.check(' console.log(b)');
typescriptParser.parsers.typescript.parse; // $ExpectType (text: string, parsers: { [parserName: string]: Parser; }, options: ParserOptions) => any
graphqlParser.parsers.graphql.parse; // $ExpectType (text: string, parsers: { [parserName: string]: Parser; }, options: ParserOptions) => any
babylonParser.parsers.babylon.parse; // $ExpectType (text: string, parsers: { [parserName: string]: Parser; }, options: ParserOptions) => any
babelParser.parsers.babel.parse; // $ExpectType (text: string, parsers: { [parserName: string]: Parser; }, options: ParserOptions) => any
htmlParser.parsers.html.parse; // $ExpectType (text: string, parsers: { [parserName: string]: Parser; }, options: ParserOptions) => any
markdownParser.parsers.markdown.parse; // $ExpectType (text: string, parsers: { [parserName: string]: Parser; }, options: ParserOptions) => any
postcssParser.parsers.postcss.parse; // $ExpectType (text: string, parsers: { [parserName: string]: Parser; }, options: ParserOptions) => any
postcssParser.parsers.css.parse; // $ExpectType (text: string, parsers: { [parserName: string]: Parser; }, options: ParserOptions) => any
yamlParser.parsers.yaml.parse; // $ExpectType (text: string, parsers: { [parserName: string]: Parser; }, options: ParserOptions) => any
prettier.format('hello world', {plugins: [typescriptParser, graphqlParser, babylonParser, htmlParser, markdownParser, postcssParser, yamlParser]});
prettier.format('hello world', {
plugins: [typescriptParser, graphqlParser, babelParser, htmlParser, markdownParser, postcssParser, yamlParser],
});

View File

@ -11,10 +11,7 @@ import { CursorOptions, CursorResult, Options, Plugin } from './';
* ```
* `-> { formatted: '1;\n', cursorOffset: 1 }`
*/
export function formatWithCursor(
source: string,
options: CursorOptions,
): CursorResult;
export function formatWithCursor(source: string, options: CursorOptions): CursorResult;
/**
* `format` is used to format text using Prettier. [Options](https://github.com/prettier/prettier#options) may be provided to override the defaults.