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

Initial support for $ReadOnlyArray #236

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const FLOW_TYPENAMES = {
$Supertype: '$supertype',
$TupleMap: '$tupleMap',
$Values: '$values',
$ReadOnlyArray: '$readOnlyArray',
Class: 'Class'
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* @flow */

export const input = `
type Demo = $ReadOnlyArray<number>;
const test: Demo = [123];
test.push(4);
`;

export const expected = `
import t from "flow-runtime";
const Demo = t.type("Demo", t.$readOnlyArray(t.number()));
const test = Demo.assert([123]);
test.push(4);
`;
9 changes: 8 additions & 1 deletion packages/flow-runtime/src/TypeContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
SymbolLiteralType,
StringType,
StringLiteralType,
$ReadOnlyArrayType,
ArrayType,
ObjectType,
ObjectTypeCallProperty,
Expand Down Expand Up @@ -222,7 +223,7 @@ export default class TypeContext {
}
// @flowIssue 252
const inferrer = this[InferrerSymbol];
(inferrer: TypeInferrer);
/*:: (inferrer: TypeInferrer); */

return inferrer.infer(input);
}
Expand Down Expand Up @@ -906,6 +907,12 @@ export default class TypeContext {
return target;
}

$readOnlyArray <T> (elementType?: Type<T>): $ReadOnlyArrayType<T> {
const target = new $ReadOnlyArrayType(this);
target.elementType = elementType || this.any();
return target;
}

tuple <T> (...types: Type<T>[]): TupleType<any> {
const target = new TupleType(this);
target.types = types;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* @flow */

import type TypeContext from '../../../TypeContext';

export function pass(t: TypeContext) {
const Demo = t.type('Demo', t.$readOnlyArray(t.number(123)));

return Demo.assert([123]);
}

export function fail(t: TypeContext) {
const Demo = t.type('Demo', t.$readOnlyArray(t.number(123)));

const arr = Demo.assert([123]);
arr.push(4);

return arr;
}
11 changes: 4 additions & 7 deletions packages/flow-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import globalContext from './globalContext';

import {
TypeParametersSymbol,
TypeSymbol
} from './symbols';
import { TypeParametersSymbol, TypeSymbol } from './symbols';

import AnyType from './types/AnyType';
import $ReadOnlyArrayType from './types/$ReadOnlyArrayType';
import ArrayType from './types/ArrayType';
import BooleanLiteralType from './types/BooleanLiteralType';
import BooleanType from './types/BooleanType';
Expand Down Expand Up @@ -64,6 +62,7 @@ export default globalContext;

export {
AnyType,
$ReadOnlyArrayType,
ArrayType,
BooleanLiteralType,
BooleanType,
Expand Down Expand Up @@ -106,7 +105,6 @@ export {
TypeTDZ,
UnionType,
VoidType,

Declaration,
TypeDeclaration,
VarDeclaration,
Expand All @@ -115,7 +113,6 @@ export {
ClassDeclaration,
ParameterizedClassDeclaration,
ExtendsDeclaration,

TypeParametersSymbol,
TypeSymbol
TypeSymbol,
};
107 changes: 107 additions & 0 deletions packages/flow-runtime/src/types/$ReadOnlyArrayType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* @flow */

import Type from '../types/Type';
import TupleType from '../types/TupleType';
import ArrayType from '../types/ArrayType';
import compareTypes from '../compareTypes';
import getErrorMessage from '../getErrorMessage';
import type Validation, {ErrorTuple, IdentifierPath} from '../Validation';

import {
inValidationCycle,
startValidationCycle,
endValidationCycle,
inToStringCycle,
startToStringCycle,
endToStringCycle,
} from '../cyclic';

export default class $ReadOnlyArrayType<T> extends Type<$ReadOnlyArray<T>> {
typeName: string = '$ReadOnlyArrayType';
elementType: Type<T>;

*errors(
validation: Validation<any>,
path: IdentifierPath,
input: any,
): Generator<ErrorTuple, void, void> {
const {context} = this;
if (!context.checkPredicate('Array', input)) {
yield [path, getErrorMessage('ERR_EXPECT_ARRAY'), this];
return;
}
if (validation.inCycle(this, input)) {
return;
}
validation.startCycle(this, input);
const {elementType} = this;
const {length} = input;

for (let i = 0; i < length; i++) {
yield* elementType.errors(validation, path.concat(i), input[i]);
}
Object.freeze(input);
validation.endCycle(this, input);
}

accepts(input: any): boolean {
const {context} = this;
if (!context.checkPredicate('Array', input)) {
return false;
}
if (inValidationCycle(this, input)) {
return true;
}
startValidationCycle(this, input);
const {elementType} = this;
const {length} = input;
for (let i = 0; i < length; i++) {
if (!elementType.accepts(input[i])) {
endValidationCycle(this, input);
return false;
}
}
endValidationCycle(this, input);
return true;
}

compareWith(input: Type<any>): -1 | 0 | 1 {
const {elementType} = this;
if (input instanceof TupleType) {
const {types} = input;
for (let i = 0; i < types.length; i++) {
const result = compareTypes(elementType, types[i]);
if (result === -1) {
return -1;
}
}
return 1;
} else if (input instanceof ArrayType) {
return compareTypes(elementType, input.elementType);
} else {
return -1;
}
}

toString(): string {
const {elementType} = this;
if (inToStringCycle(this)) {
if (typeof elementType.name === 'string') {
return `$ReadOnlyArray<$Cycle<${elementType.name}>>`;
} else {
return `$ReadOnlyArray<$Cycle<Object>>`;
}
}
startToStringCycle(this);
const output = `$ReadOnlyArray<${elementType.toString()}>`;
endToStringCycle(this);
return output;
}

toJSON() {
return {
typeName: this.typeName,
elementType: this.elementType,
};
}
}
31 changes: 16 additions & 15 deletions packages/flow-runtime/src/types/ArrayType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Type from './Type';
import TupleType from './TupleType';
import compareTypes from '../compareTypes';

import getErrorMessage from "../getErrorMessage";
import getErrorMessage from '../getErrorMessage';
import type Validation, {ErrorTuple, IdentifierPath} from '../Validation';

import {
Expand All @@ -13,14 +13,18 @@ import {
endValidationCycle,
inToStringCycle,
startToStringCycle,
endToStringCycle
endToStringCycle,
} from '../cyclic';

export default class ArrayType <T> extends Type {
export default class ArrayType<T> extends Type<Array<T>> {
typeName: string = 'ArrayType';
elementType: Type<T>;

*errors (validation: Validation<any>, path: IdentifierPath, input: any): Generator<ErrorTuple, void, void> {
*errors(
validation: Validation<any>,
path: IdentifierPath,
input: any,
): Generator<ErrorTuple, void, void> {
const {context} = this;
if (!context.checkPredicate('Array', input)) {
yield [path, getErrorMessage('ERR_EXPECT_ARRAY'), this];
Expand All @@ -39,7 +43,7 @@ export default class ArrayType <T> extends Type {
validation.endCycle(this, input);
}

accepts (input: any): boolean {
accepts(input: any): boolean {
const {context} = this;
if (!context.checkPredicate('Array', input)) {
return false;
Expand All @@ -60,7 +64,7 @@ export default class ArrayType <T> extends Type {
return true;
}

compareWith (input: Type<any>): -1 | 0 | 1 {
compareWith(input: Type<any>): -1 | 0 | 1 {
const {elementType} = this;
if (input instanceof TupleType) {
const {types} = input;
Expand All @@ -71,22 +75,19 @@ export default class ArrayType <T> extends Type {
}
}
return 1;
}
else if (input instanceof ArrayType) {
} else if (input instanceof ArrayType) {
return compareTypes(elementType, input.elementType);
}
else {
} else {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add subtyping here too

return -1;
}
}

toString (): string {
toString(): string {
const {elementType} = this;
if (inToStringCycle(this)) {
if (typeof elementType.name === 'string') {
return `Array<$Cycle<${elementType.name}>>`;
}
else {
} else {
return `Array<$Cycle<Object>>`;
}
}
Expand All @@ -96,10 +97,10 @@ export default class ArrayType <T> extends Type {
return output;
}

toJSON () {
toJSON() {
return {
typeName: this.typeName,
elementType: this.elementType
elementType: this.elementType,
};
}
}
38 changes: 19 additions & 19 deletions packages/flow-runtime/src/types/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
/* @flow */

export type TypeCreator <T> = (partial: PartialType<T>) => T;
export type TypeRevealer <T> = () => ? Type<T> | Class<T>;
export type TypeCreator<T> = (partial: PartialType<T>) => T;
export type TypeRevealer<T> = () => ?Type<T> | Class<T>;

export type FunctionBodyCreator <X, P, R> = (
partial: PartialType<(...params: P[]) => R>
export type FunctionBodyCreator<X, P, R> = (
partial: PartialType<(...params: P[]) => R>,
) => Array<ValidFunctionBody<X, P, R>>;

export type ValidFunctionBody<X, P, R>
= TypeParameter<X>
| FunctionTypeParam<X | P>
| FunctionTypeRestParam<X | P>
| FunctionTypeReturn<R>
;
export type ValidFunctionBody<X, P, R> =
| TypeParameter<X>
| FunctionTypeParam<X | P>
| FunctionTypeRestParam<X | P>
| FunctionTypeReturn<R>;

export type ObjectPropertyDict<T> = $ObjMap<T, <V>(v: Type<V>) => V>;

export type ValidObjectBody<O: {}>
= ObjectTypeCallProperty<any>
| ObjectTypeProperty<$Keys<O>, $ObjMap<O, <V>(v: Type<V>) => V>>
| ObjectTypeIndexer<*, *>
;
export type ValidObjectBody<O: {}> =
| ObjectTypeCallProperty<any>
| ObjectTypeProperty<$Keys<O>, $ObjMap<O, <V>(v: Type<V>) => V>>
| ObjectTypeIndexer<*, *>;

export type TypeConstraint = (input: any) => ? string;
export type TypeConstraint = (input: any) => ?string;

export type ApplicableType<T> = Type<T> & {
name: string;
apply <P> (...typeParameters: Type<P>[]): TypeParameterApplication<P, T>;
name: string,
apply<P>(...typeParameters: Type<P>[]): TypeParameterApplication<P, T>,
};

export type Constructor<T> = Class<T>;

import AnyType from './AnyType';
import ArrayType from './ArrayType';
import $ReadOnlyArrayType from './$ReadOnlyArrayType';
import BooleanLiteralType from './BooleanLiteralType';
import BooleanType from './BooleanType';
import EmptyType from './EmptyType';
Expand Down Expand Up @@ -78,6 +77,7 @@ import VoidType from './VoidType';
export {
AnyType,
ArrayType,
$ReadOnlyArrayType,
BooleanLiteralType,
BooleanType,
EmptyType,
Expand Down Expand Up @@ -118,5 +118,5 @@ export {
TypeReference,
TypeTDZ,
UnionType,
VoidType
VoidType,
};