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 all commits
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,15 @@
/* @flow */

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

export function pass(t: TypeContext) {
const bar = t.array(t.number()).assert([-1, 1, 2, 3]);
const baz = t.$readOnlyArray(t.number()).assert(bar);
return baz;
}

export function fail(t: TypeContext) {
const bar = t.$readOnlyArray(t.number()).assert([0, 1, 2, 3]);
const baz = t.array(t.number()).assert(bar);
return baz;
}
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,
};
2 changes: 2 additions & 0 deletions packages/flow-runtime/src/registerTypePredicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import type TypeContext from './TypeContext';

export default function registerTypePredicates (context: TypeContext) {
// TODO: make better subtyping rules
context.setPredicate('$ReadOnlyArray', (input: any) => Array.isArray(input) && Object.isFrozen(input));
context.setPredicate('Array', (input: any) => Array.isArray(input));
context.setPredicate('Map', (input: any) => input instanceof Map);
context.setPredicate('Set', (input: any) => input instanceof Set);
Expand Down
111 changes: 111 additions & 0 deletions packages/flow-runtime/src/types/$ReadOnlyArrayType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* @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]);
}
if (context.mode === 'assert') {
Object.freeze(input);
} else {
// TODO: support warning mode somehow
}
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,
};
}
}
36 changes: 20 additions & 16 deletions packages/flow-runtime/src/types/ArrayType.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import Type from './Type';
import TupleType from './TupleType';
import $ReadOnlyArrayType from './$ReadOnlyArrayType';
import compareTypes from '../compareTypes';

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

import {
Expand All @@ -13,16 +14,20 @@ 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)) {
if (!context.checkPredicate('Array', input) || context.checkPredicate('$ReadOnlyArray', input)) {
yield [path, getErrorMessage('ERR_EXPECT_ARRAY'), this];
return;
}
Expand All @@ -39,7 +44,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 +65,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 +76,21 @@ 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 if (input instanceof $ReadOnlyArrayType) {
return compareTypes(elementType, input.elementType);
} 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 +100,10 @@ export default class ArrayType <T> extends Type {
return output;
}

toJSON () {
toJSON() {
return {
typeName: this.typeName,
elementType: this.elementType
elementType: this.elementType,
};
}
}
Loading