Skip to content

Commit c403a7c

Browse files
authored
[compiler] Upstream experimental flow integration (facebook#34121)
all credit on the Flood/ code goes to @mvitousek and @jbrown215, i'm just the one upstreaming it
1 parent fa212fc commit c403a7c

File tree

8 files changed

+2232
-3
lines changed

8 files changed

+2232
-3
lines changed

compiler/packages/babel-plugin-react-compiler/src/Flood/FlowTypes.ts

Lines changed: 752 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import {CompilerError, SourceLocation} from '..';
2+
import {
3+
ConcreteType,
4+
printConcrete,
5+
printType,
6+
StructuralValue,
7+
Type,
8+
VariableId,
9+
} from './Types';
10+
11+
export function unsupportedLanguageFeature(
12+
desc: string,
13+
loc: SourceLocation,
14+
): never {
15+
CompilerError.throwInvalidJS({
16+
reason: `Typedchecker does not currently support language feature: ${desc}`,
17+
loc,
18+
});
19+
}
20+
21+
export type UnificationError =
22+
| {
23+
kind: 'TypeUnification';
24+
left: ConcreteType<Type>;
25+
right: ConcreteType<Type>;
26+
}
27+
| {
28+
kind: 'StructuralUnification';
29+
left: StructuralValue;
30+
right: ConcreteType<Type>;
31+
};
32+
33+
function printUnificationError(err: UnificationError): string {
34+
if (err.kind === 'TypeUnification') {
35+
return `${printConcrete(err.left, printType)} is incompatible with ${printConcrete(err.right, printType)}`;
36+
} else {
37+
return `structural ${err.left.kind} is incompatible with ${printConcrete(err.right, printType)}`;
38+
}
39+
}
40+
41+
export function raiseUnificationErrors(
42+
errs: null | Array<UnificationError>,
43+
loc: SourceLocation,
44+
): void {
45+
if (errs != null) {
46+
if (errs.length === 0) {
47+
CompilerError.invariant(false, {
48+
reason: 'Should not have array of zero errors',
49+
loc,
50+
});
51+
} else if (errs.length === 1) {
52+
CompilerError.throwInvalidJS({
53+
reason: `Unable to unify types because ${printUnificationError(errs[0])}`,
54+
loc,
55+
});
56+
} else {
57+
const messages = errs
58+
.map(err => `\t* ${printUnificationError(err)}`)
59+
.join('\n');
60+
CompilerError.throwInvalidJS({
61+
reason: `Unable to unify types because:\n${messages}`,
62+
loc,
63+
});
64+
}
65+
}
66+
}
67+
68+
export function unresolvableTypeVariable(
69+
id: VariableId,
70+
loc: SourceLocation,
71+
): never {
72+
CompilerError.throwInvalidJS({
73+
reason: `Unable to resolve free variable ${id} to a concrete type`,
74+
loc,
75+
});
76+
}
77+
78+
export function cannotAddVoid(explicit: boolean, loc: SourceLocation): never {
79+
if (explicit) {
80+
CompilerError.throwInvalidJS({
81+
reason: `Undefined is not a valid operand of \`+\``,
82+
loc,
83+
});
84+
} else {
85+
CompilerError.throwInvalidJS({
86+
reason: `Value may be undefined, which is not a valid operand of \`+\``,
87+
loc,
88+
});
89+
}
90+
}
91+
92+
export function unsupportedTypeAnnotation(
93+
desc: string,
94+
loc: SourceLocation,
95+
): never {
96+
CompilerError.throwInvalidJS({
97+
reason: `Typedchecker does not currently support type annotation: ${desc}`,
98+
loc,
99+
});
100+
}
101+
102+
export function checkTypeArgumentArity(
103+
desc: string,
104+
expected: number,
105+
actual: number,
106+
loc: SourceLocation,
107+
): void {
108+
if (expected !== actual) {
109+
CompilerError.throwInvalidJS({
110+
reason: `Expected ${desc} to have ${expected} type parameters, got ${actual}`,
111+
loc,
112+
});
113+
}
114+
}
115+
116+
export function notAFunction(desc: string, loc: SourceLocation): void {
117+
CompilerError.throwInvalidJS({
118+
reason: `Cannot call ${desc} because it is not a function`,
119+
loc,
120+
});
121+
}
122+
123+
export function notAPolymorphicFunction(
124+
desc: string,
125+
loc: SourceLocation,
126+
): void {
127+
CompilerError.throwInvalidJS({
128+
reason: `Cannot call ${desc} with type arguments because it is not a polymorphic function`,
129+
loc,
130+
});
131+
}

0 commit comments

Comments
 (0)