Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS/TS] add tests for nullness supports #4070

Merged
merged 5 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .fantomasignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
src/fcs-fable
tests/**
tests_external/**

!tests/Js/Main/Nullness.fs
2 changes: 2 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* [Python] Add support for `nullArgCheck`(by @MangelMaxime)
* [All] Add support for F# `nullness` (by @MangelMaxime)
* [JS/TS] Add support for `Unchecked.nonNull` (by @MangelMaxime)

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* [Python] Add support for `nullArgCheck`(by @MangelMaxime)
* [All] Add support for F# `nullness` (by @MangelMaxime)
* [JS/TS] Add support for `Unchecked.nonNull` (by @MangelMaxime)

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions src/Fable.Compiler/ProjectCracker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ let private extractUsefulOptionsAndSources
accSources, line :: accOptions
else
accSources, accOptions
// Only forward the nullness flag if it is coming from the main project
elif line.StartsWith("--checknulls+", StringComparison.Ordinal) && isMainProj then
accSources, line :: accOptions
elif
line.StartsWith("--nowarn", StringComparison.Ordinal)
|| line.StartsWith("--warnon", StringComparison.Ordinal)
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2979,6 +2979,7 @@ let unchecked (com: ICompiler) (ctx: Context) r t (i: CallInfo) (_: Expr option)
| "Hash", [ arg ] -> structuralHash com r arg |> Some
| "Equals", [ arg1; arg2 ] -> equals com ctx r true arg1 arg2 |> Some
| "Compare", [ arg1; arg2 ] -> compare com ctx r arg1 arg2 |> Some
| "NonNull", [ arg ] -> arg |> Some
| _ -> None

let enums (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
Expand Down
1 change: 1 addition & 0 deletions tests/Js/Main/Fable.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<Compile Include="TimeOnlyTests.fs" />
<Compile Include="TimeSpanTests.fs" />
<Compile Include="ListCollectorTests.fs" />
<Compile Include="Nullness.fs" />
<Compile Include="Main.fs" />
</ItemGroup>

Expand Down
90 changes: 90 additions & 0 deletions tests/Js/Main/Nullness.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
module Fable.Tests.Nullness

open Util.Testing
open Fable.Core.JsInterop

type ABNull =
| A
| B of (string | null)

let tests =
testList
"Nullness"
[
testCase
"nullArgCheck"
(fun () ->
let ex =
try
nullArgCheck "arg" null
with e ->
e

equal "Value cannot be null. (Parameter 'arg')" ex.Message
)

testCase "Null active pattern works"
<| fun () ->
let getLength abnull =
match abnull with
| A -> 0
| B Null -> 0
| B(NonNull s) -> s.Length // `s` is derived to be `string`

equal (getLength A) 0
equal (getLength (B null)) 0

testCase "NonNull active pattern works"
<| fun () ->
let getLength abnull =
match abnull with
| A -> 0
| B Null -> 0
| B(NonNull s) -> s.Length // `s` is derived to be `string`

equal (getLength (B "hello")) 5

testCase "works with generics"
<| fun _ ->
// Generic code, note 'T must be constrained to be a reference type
let findOrNull (index: int) (list: 'T list) : 'T | null when 'T: not struct =
match List.tryItem index list with
| Some item -> item
| None -> null

equal (findOrNull 1 [ "a"; "b"; "c" ]) "b"
equal (findOrNull 3 [ "a"; "b"; "c" ]) null

#if FABLE_COMPILER
testCase "works for interop with undefined"
<| fun () ->
let maybeUndefined (value: string) : (string | null) = importMember "./js/nullness.js"

match maybeUndefined "ok" with
| NonNull _ -> equal true true
| Null -> equal true false

match maybeUndefined "foo" with
| NonNull _ -> equal true false
| Null -> equal true true

testCase "works for interop with null"
<| fun () ->
let maybeNull (value: string) : (string | null) = importMember "./js/nullness.js"

match maybeNull "ok" with
| NonNull _ -> equal true true
| Null -> equal true false

match maybeNull "foo" with
| NonNull _ -> equal true false
| Null -> equal true true
#endif

testCase "Unchecked.nonNull works"
<| fun () ->
let toUpper (text: string | null) =
(Unchecked.nonNull text).ToUpperInvariant()

equal (toUpper "hello") "HELLO"
]
17 changes: 17 additions & 0 deletions tests/Js/Main/js/nullness.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function maybeUndefined(value) {
if (value === "ok") {
return value;
}
else {
return undefined;
}
}

export function maybeNull(value) {
if (value === "ok") {
return value;
}
else {
return null;
}
}
Loading