Skip to content

Commit

Permalink
Start porting to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Aug 24, 2020
1 parent a05e9fd commit d3a4ff3
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/complete.js → src/complete.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as recast from "recast";

let fs = require("fs");
let path = require("path");
let vm2 = require("vm2");
let recast = require("recast");
let _ = require("lodash");

let b = recast.types.builders;
let n = recast.types.namedTypes;

function findFunc(ast, funcName) {
function findFunc(ast, funcName: string) {
for (let i = 0; i < ast.program.body.length; i++) {
let node = ast.program.body[i];
if (n.FunctionDeclaration.check(node)) {
Expand All @@ -18,7 +19,7 @@ function findFunc(ast, funcName) {
}
}

function appendReturn(block, name) {
function appendReturn(block, name: string) {
let id = b.identifier(name);
let stmts = block.body;
stmts = stmts.concat([b.returnStatement(id)]);
Expand Down Expand Up @@ -72,11 +73,11 @@ function sandboxEval(srcCode, srcPath, testCode, testPath) {
});
}

function parse(code) {
function parse(code: string): any {
return recast.parse(code);
}

function evalForOutput(srcPath, funcName, testPath) {
function evalForOutput(srcPath: string, funcName: string, testPath: string) {
srcPath = path.resolve(srcPath);

let src = fs.readFileSync(srcPath, "utf8");
Expand Down Expand Up @@ -117,7 +118,7 @@ function evalForOutput(srcPath, funcName, testPath) {
return found;
}

function completeFromTest(srcPath, targetFunc, testPath) {
function completeFromTest(srcPath: string, targetFunc: string, testPath: string) {
let found = evalForOutput(srcPath, targetFunc, testPath);
if (found === null) {
// eslint-disable-next-line no-console
Expand All @@ -130,4 +131,8 @@ function completeFromTest(srcPath, targetFunc, testPath) {
}
}

completeFromTest("examples/roman_numerals.js", "toNumber", "examples/roman_numerals.test.js");
completeFromTest(
"examples/roman_numerals.js",
"toNumber",
"examples/roman_numerals.test.js"
);

0 comments on commit d3a4ff3

Please sign in to comment.