Skip to content

Commit

Permalink
fix: Fix null/undefined not being parsed as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Aug 6, 2024
1 parent f4f2757 commit ef50863
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
8 changes: 7 additions & 1 deletion packages/nitrogen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ let generatedSpecs = 0
const baseDirectory = getBaseDirectory()

// The TS project
const project = new Project({})
const project = new Project({
compilerOptions: {
strict: true,
strictNullChecks: true,
noUncheckedIndexedAccess: true,
},
})
project.addSourceFilesAtPaths(path.join(baseDirectory, '/**/*.nitro.ts'))

// Loop through all source files to log them
Expand Down
3 changes: 1 addition & 2 deletions packages/nitrogen/src/syntax/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ export class Method implements CodeNode {
const [method] = args
this.name = method.getSymbolOrThrow().getEscapedName()
const returnType = method.getReturnType()
const isOptional = returnType.isNullable()
this.returnType = createType(returnType, isOptional)
this.returnType = createType(returnType, returnType.isNullable())
this.parameters = method.getParameters().map((p) => new Parameter(p))
}
}
Expand Down
17 changes: 10 additions & 7 deletions packages/nitrogen/src/syntax/createType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,17 @@ export function createType(type: TSMorphType, isOptional: boolean): Type {
return new EnumType(typename, type)
} else {
// It consists of different types - that means it's a variant!
const variants = type.getUnionTypes().map((t) => createType(t, false))
const isOptionalVariant = variants.some((t) => t.kind === 'null')
const variant = new VariantType(variants.filter((v) => v.kind !== 'null'))
if (isOptionalVariant) {
return new OptionalType(variant)
} else {
return variant
const variants = type
.getUnionTypes()
// Filter out any nulls or undefineds, as those are already treated as `isOptional`.
.filter((t) => !t.isNull() && !t.isUndefined() && !t.isVoid())
.map((t) => createType(t, false))
if (variants.length === 1) {
// It's just one type with undefined/null varians - so we treat it like a simple optional.
return variants[0]!
}

return new VariantType(variants)
}
} else if (type.isInterface()) {
// It references another interface/type, either a simple struct, or another HybridObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ namespace margelo::nitro::image {
virtual void setStringValue(const std::string& stringValue) = 0;
virtual int64_t getBigintValue() = 0;
virtual void setBigintValue(int64_t bigintValue) = 0;
virtual std::string getStringOrUndefined() = 0;
virtual void setStringOrUndefined(const std::string& stringOrUndefined) = 0;
virtual std::string getStringOrNull() = 0;
virtual void setStringOrNull(const std::string& stringOrNull) = 0;
virtual std::optional<std::string> getStringOrUndefined() = 0;
virtual void setStringOrUndefined(const std::optional<std::string>& stringOrUndefined) = 0;
virtual std::optional<std::string> getStringOrNull() = 0;
virtual void setStringOrNull(const std::optional<std::string>& stringOrNull) = 0;
virtual std::optional<std::string> getOptionalString() = 0;
virtual void setOptionalString(const std::optional<std::string>& optionalString) = 0;
virtual double getValueThatWillThrowOnAccess() = 0;
Expand All @@ -89,7 +89,7 @@ namespace margelo::nitro::image {
virtual std::shared_ptr<AnyMap> mapRoundtrip(const std::shared_ptr<AnyMap>& map) = 0;
virtual double funcThatThrows() = 0;
virtual std::string tryOptionalParams(double num, bool boo, const std::optional<std::string>& str) = 0;
virtual std::string tryMiddleParam(double num, bool boo, const std::string& str) = 0;
virtual std::string tryMiddleParam(double num, const std::optional<std::variant<bool>>& boo, const std::string& str) = 0;
virtual std::variant<std::string, double> passVariant(const std::variant<std::string, double, bool, std::vector<double>, std::vector<std::string>>& either) = 0;
virtual std::tuple<double, double, double> flip(const std::tuple<double, double, double>& tuple) = 0;
virtual std::tuple<double, std::string, bool> passTuple(const std::tuple<double, std::string, bool>& tuple) = 0;
Expand All @@ -103,7 +103,7 @@ namespace margelo::nitro::image {
virtual std::future<void> getValueFromJsCallback(const Func_std__future_std__string_& callback, const Func_void_std__string& andThenCall) = 0;
virtual Car getCar() = 0;
virtual bool isCarElectric(const Car& car) = 0;
virtual Person getDriver(const Car& car) = 0;
virtual std::optional<Person> getDriver(const Car& car) = 0;
virtual std::shared_ptr<ArrayBuffer> createArrayBuffer() = 0;
virtual double getBufferLastItem(const std::shared_ptr<ArrayBuffer>& buffer) = 0;
virtual void setAllValuesTo(const std::shared_ptr<ArrayBuffer>& buffer, double value) = 0;
Expand Down

0 comments on commit ef50863

Please sign in to comment.