Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Feb 1, 2024
1 parent 5bfd2dd commit c4389a4
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions utils/graphile-export/src/exportSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ function funcToAst(
locationHint: string,
_nameHint: string,
): t.FunctionExpression | t.ArrowFunctionExpression {
const [ast, path] = _funcToAst(fn, locationHint, _nameHint);
const path = _funcToAst(fn, locationHint, _nameHint);

const externalReferences = new Set<string>();

Expand Down Expand Up @@ -1341,20 +1341,18 @@ function funcToAst(
);
}

return ast;
return path.node;
}

function parseExpressionViaDoc(funcString: string) {
const doc = parse(`const f = ${funcString}`, {
sourceType: "module",
plugins: ["typescript"],
});
let result: null | [t.Expression, NodePath<t.VariableDeclaration>] =
null as any;
let result: null | NodePath<t.Expression> = null as any;
traverse(doc, {
VariableDeclaration(path) {
const ast = path.node.declarations[0].init!;
result = [ast, path];
result = path.get("declarations.0.init") as NodePath<t.Expression>;
path.stop();
},
});
Expand All @@ -1369,7 +1367,8 @@ function parseExpressionViaDoc(funcString: string) {
function _funcToAst(fn: AnyFunction, locationHint: string, _nameHint: string) {
const funcString = fn.toString().trim();
try {
const [result, path] = parseExpressionViaDoc(funcString);
const path = parseExpressionViaDoc(funcString);
const result = path.node;
if (
result.type !== "FunctionExpression" &&
result.type !== "ArrowFunctionExpression"
Expand All @@ -1391,7 +1390,7 @@ Object.defineProperty(${
`Expected FunctionExpression or ArrowFunctionExpression but saw ${result.type}`,
);
}
return [result, path] as const;
return path as NodePath<typeof result>;
} catch (e) {
if (e.retry === false) {
throw e;
Expand All @@ -1408,7 +1407,8 @@ Object.defineProperty(${
const modifiedDefinition = funcString.startsWith("async ")
? "async function " + funcString.slice(6)
: "function " + funcString;
const [result, path] = parseExpressionViaDoc(modifiedDefinition);
const path = parseExpressionViaDoc(modifiedDefinition);
const result = path.node;
if (
result.type !== "FunctionExpression" &&
result.type !== "ArrowFunctionExpression"
Expand All @@ -1417,7 +1417,7 @@ Object.defineProperty(${
`Expected FunctionExpression or ArrowFunctionExpression but saw ${result.type}`,
);
}
return [result, path] as const;
return path as NodePath<typeof result>;
} catch {
throw new Error(
`Function export error at ${locationHint} - failed to process function definition '${trimDef(
Expand Down

0 comments on commit c4389a4

Please sign in to comment.