Fix Prettier deprecations and typings post upgrade (#1307)

Summary:
In #1194, we upgraded Prettier from 1.13.4 to 1.18.2, but this upgrades
past <https://github.com/prettier/prettier/pull/5647>, which was first
released in Prettier 1.16.0. This commit fixes the uses of deprecated
code introduced as a result. It also upgrades the type definitions to
match, via `flow-typed install prettier@1.18.2`.

Addresses part of #1308.

Test Plan:
Prior to this commit, running `yarn unit` would print

```
    console.warn node_modules/prettier/index.js:7934
      { parser: "babylon" } is deprecated; we now treat it as { parser: "babel" }.
```

in two test cases; it no longer prints any such warnings. Furthermore,
running `git grep 'parser.*babylon'` no longer finds any matches.

wchargin-branch: prettier-deprecations
This commit is contained in:
William Chargin 2019-08-22 09:00:25 -07:00 committed by GitHub
parent 141a0a23d2
commit c162813a5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 28 deletions

View File

@ -1,26 +1,48 @@
// flow-typed signature: 066c92e9ccb5f0711df8d73cbca837d6
// flow-typed version: 9e32affdbd/prettier_v1.x.x/flow_>=v0.56.x
// flow-typed signature: ec743a1b5c1197353e0849812930f32a
// flow-typed version: c6154227d1/prettier_v1.x.x/flow_>=v0.104.x
declare module "prettier" {
declare export type AST = Object;
declare export type Doc = Object;
declare export type FastPath = Object;
declare export type AST = { [key: string]: any, ... };
declare export type Doc = {
[key: string]: any,
...
};
declare export type FastPath<T = any> = {
stack: any[],
getName(): null | string | number | Symbol,
getValue(): T,
getNode(count?: number): null | T,
getParentNode(count?: number): null | T,
call<U>(callback: (path: FastPath<T>) => U, ...names: Array<string | number | Symbol>): U,
each(callback: (path: FastPath<T>) => void, ...names: Array<string | number | Symbol>): void,
map<U>(callback: (path: FastPath<T>, index: number) => U, ...names: Array<string | number | Symbol>): U[],
...
};
declare export type PrettierParserName =
| "babylon"
| "babylon" // deprecated
| "babel"
| "babel-flow"
| "flow"
| "typescript"
| "postcss"
| "postcss" // deprecated
| "css"
| "less"
| "scss"
| "json"
| "json5"
| "json-stringify"
| "graphql"
| "markdown"
| "vue";
| "vue"
| "html"
| "angular"
| "mdx"
| "yaml";
declare export type PrettierParser = {
[name: PrettierParserName]: (text: string, options?: Object) => AST
[name: PrettierParserName]: (text: string, options?: { [key: string]: any, ... }) => AST,
...
};
declare export type CustomParser = (
@ -51,31 +73,51 @@ declare module "prettier" {
declare export type Plugin = {
languages: SupportLanguage,
parsers: { [parserName: string]: Parser },
printers: { [astFormat: string]: Printer }
parsers: { [parserName: string]: Parser, ... },
printers: { [astFormat: string]: Printer, ... },
options?: SupportOption[],
...
};
declare export type Parser = {
parse: (
text: string,
parsers: { [parserName: string]: Parser },
options: Object
parsers: { [parserName: string]: Parser, ... },
options: { [key: string]: any, ... }
) => AST,
astFormat: string
astFormat: string,
hasPragma?: (text: string) => boolean,
locStart: (node: any) => number,
locEnd: (node: any) => number,
preprocess?: (text: string, options: { [key: string]: any, ... }) => string,
...
};
declare export type Printer = {
print: (
path: FastPath,
options: Object,
print: (path: FastPath) => Doc
path: FastPath<>,
options: { [key: string]: any, ... },
print: (path: FastPath<>) => Doc
) => Doc,
embed: (
path: FastPath,
print: (path: FastPath) => Doc,
textToDoc: (text: string, options: Object) => Doc,
options: Object
) => ?Doc
path: FastPath<>,
print: (path: FastPath<>) => Doc,
textToDoc: (text: string, options: { [key: string]: any, ... }) => Doc,
options: { [key: string]: any, ... }
) => ?Doc,
insertPragma?: (text: string) => string,
massageAstNode?: (node: any, newNode: any, parent: any) => any,
hasPrettierIgnore?: (path: FastPath<>) => boolean,
canAttachComment?: (node: any) => boolean,
willPrintOwnComments?: (path: FastPath<>) => boolean,
printComments?: (path: FastPath<>, print: (path: FastPath<>) => Doc, options: { [key: string]: any, ... }, needsSemi: boolean) => Doc,
handleComments?: {
ownLine?: (commentNode: any, text: string, options: { [key: string]: any, ... }, ast: any, isLastComment: boolean) => boolean,
endOfLine?: (commentNode: any, text: string, options: { [key: string]: any, ... }, ast: any, isLastComment: boolean) => boolean,
remaining?: (commentNode: any, text: string, options: { [key: string]: any, ... }, ast: any, isLastComment: boolean) => boolean,
...
},
...
};
declare export type CursorOptions = {|
@ -121,7 +163,8 @@ declare module "prettier" {
extensions: Array<string>,
filenames?: Array<string>,
linguistLanguageId: number,
vscodeLanguageIds: Array<string>
vscodeLanguageIds: Array<string>,
...
};
declare export type SupportOption = {|
@ -168,7 +211,8 @@ declare module "prettier" {
formatWithCursor: (source: string, options: CursorOptions) => CursorResult,
resolveConfig: {
(filePath: string, options?: ResolveConfigOptions): Promise<?Options>,
sync(filePath: string, options?: ResolveConfigOptions): ?Options
sync(filePath: string, options?: ResolveConfigOptions): ?Options,
...
},
clearConfigCache: () => void,
getSupportInfo: (version?: string) => SupportInfo

View File

@ -5,7 +5,7 @@ import * as Schema from "./schema";
describe("graphql/generateFlowTypes", () => {
function run(schema: Schema.Schema): string {
return generateFlowTypes(schema, {parser: "babylon", trailingComma: "es5"});
return generateFlowTypes(schema, {parser: "babel", trailingComma: "es5"});
}
describe("generateFlowTypes", () => {
@ -55,8 +55,8 @@ describe("graphql/generateFlowTypes", () => {
const schema = s.schema({
E: s.enum(["ONE", "TWO"]),
});
const options1 = {parser: "babylon", singleQuote: false};
const options2 = {parser: "babylon", singleQuote: true};
const options1 = {parser: "babel", singleQuote: false};
const options2 = {parser: "babel", singleQuote: true};
const output1 = generateFlowTypes(schema, options1);
const output2 = generateFlowTypes(schema, options2);
expect(output1).not.toEqual(output2);

View File

@ -7,7 +7,7 @@ import schema from "./schema";
export default function generateGraphqlFlowTypes() {
const prettierOptions = {
...{parser: "babylon"},
...{parser: "babel"},
...(prettier.resolveConfig.sync(__filename) || {}),
};
return generateFlowTypes(schema(), prettierOptions);