-
Notifications
You must be signed in to change notification settings - Fork 57
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
goodmind
wants to merge
4
commits into
gajus:master
Choose a base branch
from
goodmind:readOnlyArray-type
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...s/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/199-support-$ReadOnlyArray.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/flow-runtime/src/__tests__/__fixtures__/bugs/199-support-$ReadOnlyArray.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add subtyping here too