-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
853 additions
and
24 deletions.
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
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
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,7 @@ | ||
[ignore] | ||
|
||
[include] | ||
|
||
[libs] | ||
|
||
[options] |
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,4 @@ | ||
// @flow | ||
|
||
export function mono(a: number, b: {c: number}) { return a + b.c; }; | ||
export function poly<T: number, U: T, V: U> (a: V) { return a; } |
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,21 @@ | ||
// @flow | ||
|
||
export class Base<A, B, C> { | ||
// Testing infinite type recursion | ||
baseInst: Base<number, string, mixed>; | ||
|
||
// Testing forward references | ||
childInst: Child<string, number>; | ||
|
||
baseMethod(a: number, b: string) { return a; } | ||
overriddenMethod(a: {b: number, c: number}) { return a.b + a.c; } | ||
}; | ||
|
||
export class Child<A, B> extends Base<A, B, mixed> { | ||
notExported: NotExportedUsed; | ||
|
||
overriddenMethod(a: {b: number}) { return a.b; } | ||
} | ||
|
||
class NotExportedUsed {} | ||
class NotExportedNotUsed {} |
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,13 @@ | ||
// @flow | ||
|
||
export const constExport = 42; | ||
export let letExport = 43; | ||
export var varExport = 44; | ||
|
||
export type typeExport = number; | ||
|
||
type UnexportedT = string; | ||
export const unexportedAlias = ((0: any): UnexportedT); | ||
|
||
class C {} | ||
export const unexportedNominal = ((0: any): C); |
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 @@ | ||
export function addNum(a: number, b: number) { return a + b; } |
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 @@ | ||
declare class Class0 { | ||
} | ||
declare export class Base<A, B, C> { | ||
baseInst: Base<number, string, mixed>; | ||
childInst: Child<string, number>; | ||
|
||
baseMethod(a: number, b: string): number; | ||
} | ||
|
||
declare export class Child<A, B> extends Base<A, B, mixed> { | ||
notExported: Class0; | ||
} | ||
|
||
|
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,69 @@ | ||
/* @flow */ | ||
|
||
|
||
import {suite, test} from '../../tsrc/test/Tester'; | ||
|
||
export default suite(({addFile, flowCmd}) => [ | ||
test('class exports', [ | ||
addFile('named_class_exports.js'), | ||
flowCmd(['shadow-file', 'named_class_exports.js']).stdout(` | ||
declare class Class0 { | ||
} | ||
declare export class Base<A, B, C> { | ||
baseInst: Base<number, string, mixed>; | ||
childInst: Child<string, number>; | ||
baseMethod(a: number, b: string): number; | ||
overriddenMethod(a: {b: number, c: number}): number; | ||
} | ||
declare export class Child<A, B> extends Base<A, B, mixed> { | ||
notExported: Class0; | ||
overriddenMethod(a: {b: number}): number; | ||
} | ||
`) | ||
.stderr('') | ||
]), | ||
|
||
test('named variable exports', [ | ||
addFile('named_variable_exports.js'), | ||
flowCmd(['shadow-file', 'named_variable_exports.js']).stderr('').stdout(` | ||
declare class Class0 { | ||
} | ||
declare export var constExport: 42; | ||
declare export var letExport: 43; | ||
export type typeExport = number; | ||
declare export var unexportedAlias: string; | ||
declare export var unexportedNominal: Class0; | ||
declare export var varExport: 44; | ||
`) | ||
]), | ||
|
||
test('function exports', [ | ||
addFile('function_exports.js'), | ||
flowCmd(['shadow-file', 'function_exports.js']).stderr('').stdout(` | ||
declare export function mono(a: number, b: {c: number}): number; | ||
declare export function poly<T: number, U: T, V: U>(a: V): number; | ||
`) | ||
]), | ||
|
||
test('non-@flow files', [ | ||
addFile('non_flow_file.js'), | ||
flowCmd(['shadow-file', 'non_flow_file.js']).stderr('').stdout(` | ||
declare export function addNum(a: number, b: number): number; | ||
`) | ||
]), | ||
|
||
test('type errors halt and stderr', [ | ||
addFile('type_error.js'), | ||
flowCmd(['shadow-file', 'type_error.js']).stdout('').stderr(` | ||
type_error.js:3 | ||
3: export var a: string = 42; | ||
^^ number. This type is incompatible with | ||
3: export var a: string = 42; | ||
^^^^^^ string | ||
Found 1 error | ||
There must be no type errors in order to generate a shadow file! | ||
`) | ||
]), | ||
]); |
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,3 @@ | ||
// @flow | ||
|
||
export var a: string = 42; |
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,66 @@ | ||
open CommandUtils | ||
|
||
let spf = Printf.sprintf | ||
|
||
let name = "shadow-file" | ||
let spec = { | ||
CommandSpec. | ||
name; | ||
doc = "Given a filename, generate a shadow (.js.flow) file."; | ||
usage = Printf.sprintf | ||
"Usage: %s %s [OPTIONS] [FILE] [FILE] [FILE]...\n\n\ | ||
e.g. %s %s foo.js > foo.js.flow\n" | ||
CommandUtils.exe_name | ||
name | ||
CommandUtils.exe_name | ||
name | ||
; | ||
args = CommandSpec.ArgSpec.( | ||
empty | ||
|> server_flags | ||
|> root_flag | ||
|> error_flags | ||
|> strip_root_flag | ||
|> anon "file" (required string) | ||
~doc:"The file for which a shadow file should be generated" | ||
) | ||
} | ||
|
||
let main option_values root error_flags strip_root file () = ( | ||
let root = guess_root ( | ||
match root with | ||
| Some root -> Some root | ||
| None -> Some (expand_path file) | ||
) in | ||
let filename = ServerProt.FileName (expand_path file) in | ||
let (in_chan, out_chan) = connect option_values root in | ||
ServerProt.cmd_to_channel out_chan (ServerProt.GEN_INTERFACES [filename]); | ||
let response = | ||
(Timeout.input_value in_chan: ServerProt.gen_interface_response) | ||
in | ||
|
||
match response with | ||
| response::[] -> ( | ||
match response with | ||
| Utils_js.Err (ServerProt.GenIface_TypecheckError (_, errors)) -> | ||
let errors = Errors.to_list errors in | ||
Errors.print_error_summary ~stderr:true ~flags:error_flags ~strip_root ~root errors; | ||
FlowExitStatus.exit | ||
~msg:"\nThere must be no type errors in order to generate a shadow file!" | ||
FlowExitStatus.Type_error; | ||
| Utils_js.Err (ServerProt.GenIface_UnexpectedError (file_path, error)) -> | ||
FlowExitStatus.exit | ||
~msg:(spf "Error: %s: %s" file_path error) | ||
FlowExitStatus.Unknown_error | ||
| Utils_js.OK (_file, interface) -> | ||
print_endline interface | ||
) | ||
| response -> | ||
let msg = spf ( | ||
"Internal Error: Expected a single interface description from the " ^^ | ||
"server, but received %d interfaces!" | ||
) (List.length response) in | ||
prerr_endline msg | ||
) | ||
|
||
let command = CommandSpec.command spec main |
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
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
Oops, something went wrong.