Skip to content
This repository has been archived by the owner on May 24, 2019. It is now read-only.

Commit

Permalink
Fix some bugs, adds -v
Browse files Browse the repository at this point in the history
Fixes some bugs found during the development of async-to-gen:

* Whitespace and interior comments near tokens could cause binary search to return undefined.
* Visitor was visiting unnessary values
* Enables multi-line REPL
  • Loading branch information
leebyron committed Aug 13, 2016
1 parent b923b59 commit 29d81b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
28 changes: 17 additions & 11 deletions flow-node
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var usage = 'Usage: flow-node [options] [ script.js ] [arguments] \n' +

'\nOptions:\n' +
' -h, --help Show this message\n' +
' -v, --version Prints the current version of flow-node\n' +
' -e, --eval script Evaluate script\n' +
' -p, --print script Evaluate script and print result\n' +
' -c, --check Syntax check script without executing\n';
Expand All @@ -29,6 +30,9 @@ while (i < process.argv.length) {
if (arg === '-h' || arg === '--help') {
process.stdout.write(usage);
process.exit(0);
} else if (arg === '-v' || arg === '--version') {
process.stdout.write('v' + require('./package').version);
process.exit(0);
} else if (arg === '-e' || arg === '--eval') {
evalScript = process.argv[i++];
if (!evalScript) {
Expand Down Expand Up @@ -59,7 +63,11 @@ if (evalScript || printScript) {
global.exports = evalModule.exports;
global.module = evalModule;
global.require = evalModule.require.bind(evalModule);
var result = eval(evalScript || printScript, global.__filename);
var result = vm.runInThisContext(
removeTypes(evalScript || printScript, { checkPragma: false }),
{ filename: global.__filename }
);

if (printScript) {
process.stdout.write((typeof result === 'string' ? result : util.inspect(result)) + '\n');
}
Expand Down Expand Up @@ -100,18 +108,16 @@ if (evalScript || printScript) {
var error;
var result;
try {
result = eval(code, filename);
} catch (err) {
error = err;
var runCode = removeTypes(code, { checkPragma: false });
try {
result = vm.runInThisContext(runCode, { filename: filename });
} catch (runError) {
error = runError;
}
} catch (transformError) {
error = repl.Recoverable ? new repl.Recoverable(transformError) : transformError;
}
callback(error, result);
}
});
}

function eval(code, filename) {
return vm.runInThisContext(
removeTypes(code, { checkPragma: false }),
{ filename: filename }
);
}
4 changes: 4 additions & 0 deletions flow-remove-types
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var usage = 'Usage: flow-remove-types [options] [sources] \n' +

'\nOptions:\n' +
' -h, --help Show this message\n' +
' -v, --version Prints the current version of flow-remove-types\n' +
' -i, --ignore Paths to ignore, Regular Expression\n' +
' -x, --extensions File extensions to transform\n' +
' -o, --out-file The file path to write transformed file to\n' +
Expand Down Expand Up @@ -71,6 +72,9 @@ while (i < process.argv.length) {
if (arg === '-h' || arg === '--help') {
process.stdout.write(usage);
process.exit(0);
} else if (arg === '-v' || arg === '--version') {
process.stdout.write('v' + require('./package').version);
process.exit(0);
} else if (arg === '-i' || arg === '--ignore') {
ignore = new RegExp(process.argv[i++]);
} else if (arg === '-x' || arg === '--extensions') {
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function visit(ast, context, visitor) {
stack = stack.prev;
} else {
var node = parent ? parent[keys[index]] : ast.program;
if (node && typeof node === 'object') {
if (node && typeof node === 'object' && (node.type || node.length && node[0].type)) {
if (node.type) {
var visitFn = visitor[node.type];
if (visitFn && visitFn(context, node, ast) === false) {
Expand Down Expand Up @@ -200,6 +200,8 @@ function findTokenIndex(tokens, offset) {
return ptr;
}
}

return ptr;
}

// Produce a string full of space characters of a given size.
Expand Down

0 comments on commit 29d81b5

Please sign in to comment.