Skip to content

Commit

Permalink
Fix type of fs.readFileSync
Browse files Browse the repository at this point in the history
Summary:
Fixes #1870

since the first override had fewer params, it would accept any of the latter branches when the latter branches' return types didn't match, rather than making the entire intersection fail.
Closes #1874

Reviewed By: samwgoldman

Differential Revision: D3366522

Pulled By: mroch

fbshipit-source-id: a18381e3ba14fafa0106dba270a0336276fe79cd
  • Loading branch information
mroch authored and Facebook Github Bot 1 committed Jun 10, 2016
1 parent fc744c2 commit 699253b
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,10 +639,10 @@ declare module "fs" {
options: { flag?: string },
callback: (err: ?Error, data: Buffer) => void
): void;
declare function readFileSync(filename: string): Buffer;
declare function readFileSync(filename: string, _: void): Buffer;
declare function readFileSync(filename: string, encoding: string): string;
declare function readFileSync(filename: string, options: { encoding: string, flag?: string }): string;
declare function readFileSync(filename: string, options: { flag?: string }): Buffer;
declare function readFileSync(filename: string, options: { encoding?: void, flag?: string }): Buffer;
declare function writeFile(
filename: string,
data: Buffer | string,
Expand Down
4 changes: 4 additions & 0 deletions tests/node_tests/fs/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ fs.readFile("file.exp", {}, (_, data) => {
/* readFileSync */

(fs.readFileSync("file.exp") : Buffer);
(fs.readFileSync("file.exp") : string); // error

(fs.readFileSync("file.exp", "blah") : string);
(fs.readFileSync("file.exp", "blah") : Buffer); // error

(fs.readFileSync("file.exp", { encoding: "blah" }) : string);
(fs.readFileSync("file.exp", { encoding: "blah" }) : Buffer); // error

(fs.readFileSync("file.exp", {}) : Buffer);
(fs.readFileSync("file.exp", {}) : string); // error
122 changes: 121 additions & 1 deletion tests/node_tests/node_tests.exp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,126 @@ child_process/execSync.js:8
^^^^^ string. This type is incompatible with
number. See lib: node.js:113

fs/fs.js:24
24: (fs.readFileSync("file.exp") : string); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^ call of method `readFileSync`. Function cannot be called on any member of intersection type
intersection. See lib: node.js:642
Member 1:
function type. See lib: node.js:642
Error:
24: (fs.readFileSync("file.exp") : string); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Buffer. This type is incompatible with
24: (fs.readFileSync("file.exp") : string); // error
^^^^^^ string
Member 2:
function intersection. See lib: node.js:644
Error:
24: (fs.readFileSync("file.exp") : string); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^ undefined (too few arguments, expected default/rest parameters). This type is incompatible with
union: object type | string. See lib: node.js:644
Member 1:
object type. See lib: node.js:644
Error:
24: (fs.readFileSync("file.exp") : string); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^ undefined (too few arguments, expected default/rest parameters). This type is incompatible with
object type. See lib: node.js:644
Member 2:
string. See lib: node.js:643
Error:
24: (fs.readFileSync("file.exp") : string); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^ undefined (too few arguments, expected default/rest parameters). This type is incompatible with
string. See lib: node.js:643
Member 3:
function type. See lib: node.js:645
Error:
24: (fs.readFileSync("file.exp") : string); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^ undefined (too few arguments, expected default/rest parameters). This type is incompatible with
object type. See lib: node.js:645

fs/fs.js:27
27: (fs.readFileSync("file.exp", "blah") : Buffer); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call of method `readFileSync`. Function cannot be called on any member of intersection type
intersection. See lib: node.js:642
Member 1:
function type. See lib: node.js:642
Error:
27: (fs.readFileSync("file.exp", "blah") : Buffer); // error
^^^^^^ string. This type is incompatible with
undefined. See lib: node.js:642
Member 2:
function intersection. See lib: node.js:644
Error:
27: (fs.readFileSync("file.exp", "blah") : Buffer); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string. This type is incompatible with
27: (fs.readFileSync("file.exp", "blah") : Buffer); // error
^^^^^^ Buffer
Member 3:
function type. See lib: node.js:645
Error:
27: (fs.readFileSync("file.exp", "blah") : Buffer); // error
^^^^^^ string. This type is incompatible with
object type. See lib: node.js:645

fs/fs.js:30
30: (fs.readFileSync("file.exp", { encoding: "blah" }) : Buffer); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call of method `readFileSync`. Function cannot be called on any member of intersection type
intersection. See lib: node.js:642
Member 1:
function type. See lib: node.js:642
Error:
30: (fs.readFileSync("file.exp", { encoding: "blah" }) : Buffer); // error
^^^^^^^^^^^^^^^^^^^^ object literal. This type is incompatible with
undefined. See lib: node.js:642
Member 2:
function intersection. See lib: node.js:644
Error:
30: (fs.readFileSync("file.exp", { encoding: "blah" }) : Buffer); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string. This type is incompatible with
30: (fs.readFileSync("file.exp", { encoding: "blah" }) : Buffer); // error
^^^^^^ Buffer
Member 3:
function type. See lib: node.js:645
Error:
inconsistent use of library definitions
string. This type is incompatible with. See lib: node.js:644
undefined. See lib: node.js:645

fs/fs.js:33
33: (fs.readFileSync("file.exp", {}) : string); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call of method `readFileSync`. Function cannot be called on any member of intersection type
intersection. See lib: node.js:642
Member 1:
function type. See lib: node.js:642
Error:
33: (fs.readFileSync("file.exp", {}) : string); // error
^^ object literal. This type is incompatible with
undefined. See lib: node.js:642
Member 2:
function intersection. See lib: node.js:644
Error:
33: (fs.readFileSync("file.exp", {}) : string); // error
^^ object literal. This type is incompatible with
union: object type | string. See lib: node.js:644
Member 1:
object type. See lib: node.js:644
Error:
property `encoding`. Property not found in. See lib: node.js:644
33: (fs.readFileSync("file.exp", {}) : string); // error
^^ object literal
Member 2:
string. See lib: node.js:643
Error:
33: (fs.readFileSync("file.exp", {}) : string); // error
^^ object literal. This type is incompatible with
string. See lib: node.js:643
Member 3:
function type. See lib: node.js:645
Error:
33: (fs.readFileSync("file.exp", {}) : string); // error
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Buffer. This type is incompatible with
33: (fs.readFileSync("file.exp", {}) : string); // error
^^^^^^ string

invalid_package_file/package.json:1
1:
^ Unexpected end of input
Expand Down Expand Up @@ -103,4 +223,4 @@ json_file/test.js:22
^^^^ undefined


Found 16 errors
Found 20 errors

0 comments on commit 699253b

Please sign in to comment.