Skip to content

Commit b752aab

Browse files
Fixed functions that are imported then re-exported causing a crash (#28)
1 parent 050d99a commit b752aab

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This changelog documents the changes between release versions.
88
Changes to be included in the next upcoming release
99

1010
- Fixed watch mode not reloading after files with compiler errors are changed [#27](https://github.com/hasura/ndc-nodejs-lambda/pull/27)
11+
- Fixed functions that are imported then re-exported causing a crash [#28](https://github.com/hasura/ndc-nodejs-lambda/pull/28)
1112

1213
## [1.2.0] - 2024-03-18
1314
- Improved error messages when unsupported enum types or unions of literal types are found, and allow these types to be used in relaxed types mode ([#17](https://github.com/hasura/ndc-nodejs-lambda/pull/17))

ndc-lambda-sdk/src/inference.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,14 @@ function deriveSchemaFromFunctions(sourceFile: ts.SourceFile, projectRootDir: st
171171
const declaration = exportedSymbol.getDeclarations()?.[0] ?? throwError("exported symbol does not have a declaration");
172172

173173
// If exported via 'export { name } from "./imported"'
174+
// or 'import { name } from "./imported"; export { name }'
174175
if (ts.isExportSpecifier(declaration)) {
175176
const identifier = declaration.name ?? throwError("export declaration didn't have an identifier");
176177
const exportTarget = typeChecker.getExportSpecifierLocalTargetSymbol(declaration) ?? throwError("export specifier does not have a local target symbol");
177-
const exportTargetDeclaration = exportTarget.valueDeclaration ?? throwError("export target symbol does not have a value declaration");
178+
const exportTargetDeclaration =
179+
exportTarget.valueDeclaration // 'export { name } from "./imported"'
180+
?? typeChecker.getAliasedSymbol(exportTarget).valueDeclaration // 'import { name } from "./imported"; export { name }'
181+
?? throwError("export target symbol does not have a value declaration");
178182
if (ts.isFunctionDeclaration(exportTargetDeclaration)) {
179183
return [[identifier.text, exportTargetDeclaration]];
180184
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { fileAChildFunction as renamedFileAChildFunction } from "./file-a"
2+
import { fileBChildFunction1, fileBChildFunction2 } from "./file-b"
3+
4+
export {
5+
renamedFileAChildFunction,
6+
fileBChildFunction1,
7+
fileBChildFunction2 as renamedFileBChildFunction2
8+
};

ndc-lambda-sdk/test/inference/re-exported-functions/re-exported-functions.test.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it } from "mocha";
22
import { assert } from "chai";
33
import { deriveSchema } from "../../../src/inference";
4-
import { BuiltInScalarTypeName, FunctionNdcKind, NullOrUndefinability } from "../../../src/schema";
4+
import { FunctionNdcKind } from "../../../src/schema";
55

66
describe("re-exported functions", function() {
77
it("supports functions re-exported from other files", function() {
@@ -206,4 +206,65 @@ describe("re-exported functions", function() {
206206
}
207207
})
208208
});
209+
210+
it("supports re-exports of functions from other files that are first imported then exported", function() {
211+
const schema = deriveSchema(require.resolve("./functions-import-then-export.ts"));
212+
213+
assert.deepStrictEqual(schema, {
214+
compilerDiagnostics: [],
215+
functionIssues: {},
216+
functionsSchema: {
217+
functions: {
218+
"renamedFileAChildFunction": {
219+
ndcKind: FunctionNdcKind.Procedure,
220+
description: null,
221+
parallelDegree: null,
222+
arguments: [],
223+
resultType: {
224+
name: "String",
225+
kind: "scalar",
226+
type: "named",
227+
}
228+
},
229+
"fileBChildFunction1": {
230+
ndcKind: FunctionNdcKind.Procedure,
231+
description: null,
232+
parallelDegree: null,
233+
arguments: [],
234+
resultType: {
235+
name: "Boolean",
236+
kind: "scalar",
237+
type: "named",
238+
}
239+
},
240+
"renamedFileBChildFunction2": {
241+
ndcKind: FunctionNdcKind.Function,
242+
description: null,
243+
parallelDegree: null,
244+
arguments: [
245+
{
246+
argumentName: "input",
247+
description: null,
248+
type: {
249+
name: "String",
250+
kind: "scalar",
251+
type: "named",
252+
}
253+
}
254+
],
255+
resultType: {
256+
name: "String",
257+
kind: "scalar",
258+
type: "named",
259+
}
260+
},
261+
},
262+
scalarTypes: {
263+
Boolean: { type: "built-in" },
264+
String: { type: "built-in" },
265+
},
266+
objectTypes: {},
267+
}
268+
})
269+
});
209270
});

0 commit comments

Comments
 (0)