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

fix(flow,ts): consistent interface and remove unnecessary indent for extends #5432

Merged
merged 5 commits into from Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/language-js/printer-estree.js
Expand Up @@ -1203,7 +1203,10 @@ function printPathNoParens(path, options, print, args) {
concat([
softline,
"extends ",
indent(join(concat([",", line]), path.map(print, "heritage"))),
(heritageDoc =>
n.heritage.length === 1 ? heritageDoc : indent(heritageDoc))(
join(concat([",", line]), path.map(print, "heritage"))
),
" "
])
)
Expand Down Expand Up @@ -1253,8 +1256,16 @@ function printPathNoParens(path, options, print, args) {
.sort((a, b) => options.locStart(a) - options.locStart(b))[0];

const parent = path.getParentNode(0);
const isFlowInterfaceLikeBody =
isTypeAnnotation &&
parent &&
(parent.type === "InterfaceDeclaration" ||
parent.type === "DeclareInterface" ||
parent.type === "DeclareClass") &&
path.getName() === "body";
const shouldBreak =
n.type === "TSInterfaceBody" ||
isFlowInterfaceLikeBody ||
(n.type === "ObjectPattern" &&
parent.type !== "FunctionDeclaration" &&
parent.type !== "FunctionExpression" &&
Expand All @@ -1274,13 +1285,6 @@ function printPathNoParens(path, options, print, args) {
options.locStart(n),
options.locStart(firstProperty)
));
const isFlowInterfaceLikeBody =
isTypeAnnotation &&
parent &&
(parent.type === "InterfaceDeclaration" ||
parent.type === "DeclareInterface" ||
parent.type === "DeclareClass") &&
path.getName() === "body";

const separator = isFlowInterfaceLikeBody
? ";"
Expand Down Expand Up @@ -2707,7 +2711,10 @@ function printPathNoParens(path, options, print, args) {
concat([
line,
"extends ",
indent(join(concat([",", line]), path.map(print, "extends")))
(extendsDoc =>
n.extends.length === 1 ? extendsDoc : indent(extendsDoc))(
join(concat([",", line]), path.map(print, "extends"))
)
j-f1 marked this conversation as resolved.
Show resolved Hide resolved
])
)
)
Expand Down Expand Up @@ -3352,20 +3359,24 @@ function printPathNoParens(path, options, print, args) {
}
parts.push(printTypeScriptModifiers(path, options, print));

const textBetweenNodeAndItsId = options.originalText.slice(
options.locStart(n),
options.locStart(n.id)
);

// Global declaration looks like this:
// (declare)? global { ... }
const isGlobalDeclaration =
n.id.type === "Identifier" &&
n.id.name === "global" &&
!/namespace|module/.test(
options.originalText.slice(
options.locStart(n),
options.locStart(n.id)
)
);
!/namespace|module/.test(textBetweenNodeAndItsId);

if (!isGlobalDeclaration) {
parts.push(isExternalModule ? "module " : "namespace ");
parts.push(
isExternalModule || /\smodule\s/.test(textBetweenNodeAndItsId)
? "module "
: "namespace "
);
}
}

Expand Down
16 changes: 12 additions & 4 deletions tests/flow/declare_export/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -182,7 +182,9 @@ declare export function getAFoo(): FooImpl;
* @flow
*/

declare export default class FooImpl { givesANum(): number }
declare export default class FooImpl {
givesANum(): number;
}

// Regression test for https://github.com/facebook/flow/issues/511
//
Expand All @@ -205,7 +207,9 @@ declare export default class Foo { givesANum(): number; };
* @flow
*/

declare export default class Foo { givesANum(): number }
declare export default class Foo {
givesANum(): number;
}

`;

Expand Down Expand Up @@ -461,7 +465,9 @@ declare export { specifierNumber3 };
declare export { groupedSpecifierNumber1, groupedSpecifierNumber2 };

declare export function givesANumber(): number;
declare export class NumberGenerator { givesANumber(): number }
declare export class NumberGenerator {
givesANumber(): number;
}

declare export var varDeclNumber1: number;
declare export var varDeclNumber2: number;
Expand Down Expand Up @@ -504,7 +510,9 @@ declare export { specifierNumber5 as specifierNumber5Renamed };
declare export { groupedSpecifierNumber3, groupedSpecifierNumber4 };

declare export function givesANumber2(): number;
declare export class NumberGenerator2 { givesANumber(): number }
declare export class NumberGenerator2 {
givesANumber(): number;
}

declare export var varDeclNumber3: number;
declare export var varDeclNumber4: number;
Expand Down
12 changes: 9 additions & 3 deletions tests/flow/export_type/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -11,7 +11,9 @@ module.exports = {}
/* @flow */

export type talias4 = number;
export interface IFoo { prop: number }
export interface IFoo {
prop: number;
}

module.exports = {};

Expand Down Expand Up @@ -117,7 +119,9 @@ export { standaloneType2 }; // Error: Missing \`type\` keyword

export type { talias1, talias2 as talias3, IFoo2 } from "./types_only2";

export interface IFoo { prop: number }
export interface IFoo {
prop: number;
}

`;

Expand All @@ -132,6 +136,8 @@ export interface IFoo2 { prop: string };

export type talias1 = number;
export type talias2 = number;
export interface IFoo2 { prop: string }
export interface IFoo2 {
prop: string;
}

`;
16 changes: 12 additions & 4 deletions tests/flow/implements/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -34,7 +34,9 @@ class C8<T> implements IPoly<T> { x: T }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* @noflow */

interface IFoo { foo: string }
interface IFoo {
foo: string;
}

class C1 implements IFoo {} // error: property \`foo\` not found
class C2 implements IFoo {
Expand All @@ -46,12 +48,16 @@ class C3 implements IFoo {

(new C1(): IFoo); // ok, we already errored at def site

interface IBar { bar: number }
interface IBar {
bar: number;
}

class C4 implements IFoo, IBar {} // error: properties \`foo\`, \`bar\` not found
(new C4(): IBar); // ok, we already errored at def site

interface IFooBar extends IFoo { bar: number }
interface IFooBar extends IFoo {
bar: number;
}

class C5 implements IFooBar {} // error: properties \`foo\`, \`bar\` not found
(new C5(): IFooBar); // ok, already errored at def site
Expand All @@ -64,7 +70,9 @@ class C6 extends C1 {}
class C7 implements C1 {} // error: C1 is a class, expected an interface

// ensure BoundT substituted appropriately
interface IPoly<T> { x: T }
interface IPoly<T> {
x: T;
}
class C8<T> implements IPoly<T> {
x: T;
}
Expand Down
57 changes: 43 additions & 14 deletions tests/flow/interface/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -4,7 +4,9 @@ exports[`import.js - flow-verify 1`] = `
interface I { x: number }
export type J = I; // workaround for export interface
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface I { x: number }
interface I {
x: number;
}
export type J = I; // workaround for export interface

`;
Expand Down Expand Up @@ -48,11 +50,15 @@ function testInterfaceName(o: I) {
(o.constructor.name: string); // ok
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare class C { x: number }
declare class C {
x: number;
}

var x: string = new C().x;

interface I { x: number }
interface I {
x: number;
}

var i = new I(); // error

Expand Down Expand Up @@ -85,20 +91,32 @@ var e: E<number> = { x: "", y: "", z: "" }; // error: x and z should be numbers
(e.y: string);
(e.z: string); // error: z is number
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface I { y: string }
interface I_ { x: number }
interface I {
y: string;
}
interface I_ {
x: number;
}
interface J extends I, I_ {}
interface K extends J {}

var k: K = { x: "", y: "" }; // error: x should be number
(k.x: string); // error: x is number
(k.y: string);

declare class C { x: number }
declare class C {
x: number;
}

interface A<Y> { y: Y }
interface A_<X> { x: X }
interface B<Z> extends A<string>, A_<Z> { z: Z }
interface A<Y> {
y: Y;
}
interface A_<X> {
x: X;
}
interface B<Z> extends A<string>, A_<Z> {
z: Z;
}
interface E<Z> extends B<Z> {}

var e: E<number> = { x: "", y: "", z: "" }; // error: x and z should be numbers
Expand All @@ -122,7 +140,9 @@ function bar(m: M) { m.x; m.y; m.z; } // OK
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import type { J } from "./import";
interface K {}
interface L extends J, K { y: string }
interface L extends J, K {
y: string;
}

function foo(l: L) {
l.x;
Expand Down Expand Up @@ -150,9 +170,16 @@ function foo(k: K) {
(k.y: number); // error: y is string in I
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface I { x: number; y: string }
interface J { y: number }
interface K extends I, J { x: string } // error: x is number in I
interface I {
x: number;
y: string;
}
interface J {
y: number;
}
interface K extends I, J {
x: string;
} // error: x is number in I
function foo(k: K) {
(k.x: number); // error: x is string in K
(k.y: number); // error: y is string in I
Expand All @@ -171,7 +198,9 @@ declare class C {

new C().bar((x: string) => { }); // error, number ~/~> string
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface I { foo(x: number): void }
interface I {
foo(x: number): void;
}
(function foo(x: number) {}: I); // error, property \`foo\` not found function

declare class C {
Expand Down
4 changes: 3 additions & 1 deletion tests/flow/new_spread/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -377,7 +377,9 @@ type O1 = { ...B };
declare var o1: O1;
(o1: { p?: number }); // ok

declare class C { [string]: number }
declare class C {
[string]: number;
}
type O2 = { ...C };
declare var o2: O2;
(o2: { [string]: number }); // ok
Expand Down
Expand Up @@ -19,9 +19,13 @@ interface B<X> extends A<X> {
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface Some<X> {}
interface Other<X> { x: X }
interface Other<X> {
x: X;
}
interface None<Y> {}
interface Nada<Y> { y: Y }
interface Nada<Y> {
y: Y;
}
interface A<X> {
foo<Y>(s: Some<X>, e: None<Y>): A<Y>;
foo<Y>(s: Some<X>, e: Nada<Y>): A<Y>;
Expand Down
8 changes: 6 additions & 2 deletions tests/flow/this_type/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -272,8 +272,12 @@ class C {
function foo(c: C): I { return c; }
function bar(c: C): J { return c; }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface I { xs: Array<this> }
interface J { f(): J }
interface I {
xs: Array<this>;
}
interface J {
f(): J;
}
class C {
xs: Array<C>;
f(): C {
Expand Down