Skip to content

Commit 1790a33

Browse files
committed
Add flow to devDeps & fix minor type errors
1 parent d9a621b commit 1790a33

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

.flowconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[options]
2+
sharedmemory.heap_size=3221225472

babel-watch.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
// @flow
23

34
'use strict';
45

@@ -34,7 +35,7 @@ function collect(val, memo) {
3435
// Plucked directly from old Babel Core
3536
// https://github.com/babel/babel/commit/0df0c696a93889f029982bf36d34346a039b1920
3637
function regexify(val) {
37-
if (!val) return new RegExp;
38+
if (!val) return new RegExp('');
3839
if (Array.isArray(val)) val = val.join("|");
3940
if (isString(val)) return new RegExp(val || "");
4041
if (isRegExp(val)) return val;
@@ -204,7 +205,7 @@ function handleChange(file) {
204205
function generateTempFilename() {
205206
const now = new Date();
206207
return path.join(os.tmpdir(), [
207-
now.getYear(), now.getMonth(), now.getDate(),
208+
now.getFullYear(), now.getMonth(), now.getDate(),
208209
'-',
209210
process.pid,
210211
'-',
@@ -224,7 +225,9 @@ function handleFileLoad(filename, callback) {
224225
if (!shouldIgnore(filename)) {
225226
compile(filename, (err, result) => {
226227
debugCompile('Compiled file: %s. Success? %s', filename, !err);
227-
if (err) {
228+
229+
if (!result && !err) err = new Error('No Result from Babel for file: ' + filename);
230+
if (err || !result) {
228231
// Intentional ignore
229232
if (err instanceof IgnoredFileError) {
230233
ignored[filename] = true;
@@ -395,9 +398,9 @@ function restartAppInternal() {
395398
watcher.add(relativeFilename);
396399
}
397400
handleFileLoad(filename, (source, sourceMap) => {
398-
const sourceBuf = new Buffer.from(source || '');
399-
const mapBuf = new Buffer.from(sourceMap ? JSON.stringify(sourceMap) : []);
400-
const lenBuf = new Buffer.alloc(4);
401+
const sourceBuf = Buffer.from(source || '');
402+
const mapBuf = Buffer.from(sourceMap ? JSON.stringify(sourceMap) : []);
403+
const lenBuf = Buffer.alloc(4);
401404
if (pipeFd) {
402405
try {
403406
lenBuf.writeUInt32BE(sourceBuf.length, 0);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"babel-watch": "./babel-watch.js"
5252
},
5353
"devDependencies": {
54-
"@babel/core": "^7.12.10"
54+
"@babel/core": "^7.12.10",
55+
"flow-bin": "^0.141.0"
5556
}
5657
}

runner.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'use strict';
1+
// @flow
22

33
const path = require('path');
44
const fs = require('fs');
@@ -10,21 +10,26 @@ let maps = {};
1010
let pipeFd;
1111
const BUFFER = new Buffer.alloc(10 * 1024);
1212

13+
// $FlowIgnore Flow doesn't recognize require.extensions
14+
const reqExtensions/*: any */ = require.extensions;
15+
1316
// Node by default uses '.js' loader to load all the files with unknown extensions
14-
const DEFAULT_LOADER = require.extensions['.js'];
17+
const DEFAULT_LOADER = reqExtensions['.js'];
1518

1619
function readLength(fd) {
1720
let bytes = 0;
1821
while (typeof bytes === 'number' && bytes !== 4) {
19-
bytes = fs.readSync(fd, BUFFER, 0, 4);
22+
// $FlowIgnore position can be null
23+
bytes = fs.readSync(fd, BUFFER, 0, 4, null);
2024
}
2125
return BUFFER.readUInt32BE(0);
2226
}
2327

2428
function readFileFromPipeSync(fd) {
2529
let length = readLength(fd);
26-
let result = new Buffer.alloc(0);
30+
let result = Buffer.alloc(0);
2731
while (length > 0) {
32+
// $FlowIgnore position can be null
2833
const newBytes = fs.readSync(fd, BUFFER, 0, Math.min(BUFFER.length, length));
2934
length -= newBytes;
3035
result = Buffer.concat([result, BUFFER], result.length + newBytes);
@@ -40,6 +45,8 @@ function babelWatchLoader(module_, filename, defaultHandler) {
4045
// a named unix pipe (mkfifo). All the alternative ways would
4146
// require writing native code which usually brings large
4247
// dependencies to the project and I prefer to avoid that
48+
//
49+
// $FlowIgnore we know process.send exists b/c this is a child process
4350
process.send({
4451
event: 'babel-watch-filename',
4552
filename: filename,
@@ -55,8 +62,8 @@ function babelWatchLoader(module_, filename, defaultHandler) {
5562
}
5663

5764
function registerExtension(ext) {
58-
const defaultHandler = require.extensions[ext] || DEFAULT_LOADER;
59-
require.extensions[ext] = (module_, filename) => {
65+
const defaultHandler = reqExtensions[ext] || DEFAULT_LOADER;
66+
reqExtensions[ext] = (module_, filename) => {
6067
// ignore node_modules by default. don't you dare contacting the parent process!
6168
if (filename.split(path.sep).indexOf('node_modules') < 0) {
6269
babelWatchLoader(module_, filename, defaultHandler);
@@ -74,12 +81,12 @@ function registerExtension(ext) {
7481
}
7582

7683
function replaceExtensionHooks(extensions) {
77-
for (const ext in require.extensions) {
84+
for (const ext in reqExtensions) {
7885
registerExtension(ext);
7986
}
8087
for (let i = 0; i < extensions.length; i++) {
8188
const ext = extensions[i];
82-
if (!(ext in require.extensions)) {
89+
if (!(ext in reqExtensions)) {
8390
registerExtension(ext);
8491
}
8592
}
@@ -108,5 +115,6 @@ process.on('message', (options) => {
108115

109116
pipeFd = fs.openSync(options.pipe, 'r');
110117
process.argv = ["node"].concat(options.args);
118+
// $FlowIgnore doesn't recognize 'module' as it is internal
111119
require('module').runMain();
112120
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ fill-range@^7.0.1:
302302
dependencies:
303303
to-regex-range "^5.0.1"
304304

305+
flow-bin@^0.141.0:
306+
version "0.141.0"
307+
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.141.0.tgz#f9e33ad29392824823c97b6db7de5ab972c7f872"
308+
integrity sha512-NxaECTjIWfs2Y91GuA1PlgPd5uCulZcqR9wiXRg6n7/AbmvVetM2ewoGxCKxJm7wIml3f0/5KXIZvZa/3msqXg==
309+
305310
fsevents@~2.1.2:
306311
version "2.1.2"
307312
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.1.2.tgz#4c0a1fb34bc68e543b4b82a9ec392bfbda840805"

0 commit comments

Comments
 (0)