-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
flow shadow-file
#2184
Closed
Closed
flow shadow-file
#2184
Changes from all commits
Commits
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
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
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,76 @@ | ||
(** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the "flow" directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
*) | ||
|
||
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 ~out_channel:stderr ~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
Oops, something went wrong.
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.
missing copyright header