Skip to content

Commit 8c1dcb3

Browse files
authored
feat(js/ts): warn when a date/time API drops sub-millisecond precision (#5339)
1 parent a8f8a71 commit 8c1dcb3

3 files changed

Lines changed: 194 additions & 0 deletions

File tree

‎src/Fable.Transforms/Global/WarningCodes.fs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ let regexFlagUnsupported (flag: string) =
136136
let duplicateCompiledName (unionName: string) (caseName: string) (otherCaseName: string) (compiledName: string) =
137137
"FABLE0121", $"Cases '%s{caseName}' and '%s{otherCaseName}' of '%s{unionName}' both compile to '%s{compiledName}'"
138138

139+
/// JS/TS without Temporal: a ticks or microseconds argument given to a `DateTime`, `DateTimeOffset`,
140+
/// `TimeSpan` or `TimeOnly` API that truncates it to whole milliseconds.
141+
let subMillisecondPrecisionDropped (typeName: string) =
142+
"FABLE0122",
143+
$"%s{typeName} precision is limited to milliseconds, the sub-millisecond part is dropped. Use --test:js-temporal to keep it."
144+
139145
/// Every code the compiler can emit, both bands. A directive naming anything else is reported as
140146
/// a typo, so a new warning MUST be added to this list as well as defined above.
141147
let knownCodes =
@@ -166,6 +172,7 @@ let knownCodes =
166172
staticDoBindingsDisabled
167173
regexFlagUnsupported ""
168174
duplicateCompiledName "" "" "" ""
175+
subMillisecondPrecisionDropped ""
169176
]
170177
|> List.map fst
171178
|> Set.ofList

‎src/Fable.Transforms/Replacements.fs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3955,8 +3955,53 @@ let ignoreFormatProvider com (ctx: Context) r meth args =
39553955
[ input; defVal ]
39563956
| _ -> args
39573957

3958+
let private warnOnSubMillisecondPrecisionLoss
3959+
(com: ICompiler)
3960+
(ctx: Context)
3961+
r
3962+
(typeName: string)
3963+
(i: CallInfo)
3964+
(args: Expr list)
3965+
=
3966+
let ticksPerMillisecond = 10000L
3967+
let microsecondsPerMillisecond = 1000L
3968+
3969+
let isInt32 (e: Expr) =
3970+
match e.Type with
3971+
| Number(Int32, NumberInfo.Empty) -> true
3972+
| _ -> false
3973+
3974+
let lossyArg =
3975+
match typeName, i.CompiledName, args with
3976+
| ("DateTime" | "DateTimeOffset"), ".ctor", (ExprType(Number(Int64, _)) as ticks) :: _
3977+
| ("DateTime" | "DateTimeOffset"), "AddTicks", [ ticks ]
3978+
| "TimeSpan", (".ctor" | "FromTicks"), [ ticks ]
3979+
| "TimeOnly", ".ctor", [ ExprType(Number(Int64, _)) as ticks ] -> Some(ticks, ticksPerMillisecond)
3980+
| ("DateTime" | "DateTimeOffset"), ".ctor", _ :: _ :: _ :: _ :: _ :: _ :: _ :: us :: _ when isInt32 us ->
3981+
Some(us, microsecondsPerMillisecond)
3982+
| "TimeSpan", ".ctor", [ _; _; _; _; _; us ]
3983+
| "TimeOnly", ".ctor", [ _; _; _; _; us ] -> Some(us, microsecondsPerMillisecond)
3984+
| _ -> None
3985+
3986+
match lossyArg with
3987+
| _ when com.Options.JsTemporal -> ()
3988+
| None -> ()
3989+
| Some(MaybeCasted(Call(Import({ Selector = "getTicks" }, _, _), _, _, _)), _) -> ()
3990+
| Some(Value(NumberConstant(NumberValue.Int64 value, _), _), unitsPerMillisecond) when
3991+
value % unitsPerMillisecond = 0L
3992+
->
3993+
()
3994+
| Some(Value(NumberConstant(NumberValue.Int32 value, _), _), unitsPerMillisecond) when
3995+
int64 value % unitsPerMillisecond = 0L
3996+
->
3997+
()
3998+
| Some _ ->
3999+
WarningCodes.subMillisecondPrecisionDropped typeName
4000+
|> addWarningWithCode com ctx.InlinePath r
4001+
39584002
let dateTime (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
39594003
let moduleName = dateTimeModule com
4004+
warnOnSubMillisecondPrecisionLoss com ctx r "DateTime" i args
39604005

39614006
match i.CompiledName with
39624007
| ".ctor" ->
@@ -4040,6 +4085,7 @@ let dateTime (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
40404085

40414086
let dateTimeOffset (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
40424087
let moduleName = dateTimeOffsetModule com
4088+
warnOnSubMillisecondPrecisionLoss com ctx r "DateTimeOffset" i args
40434089

40444090
match i.CompiledName with
40454091
| ".ctor" ->
@@ -4215,6 +4261,7 @@ let dateOnly (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
42154261

42164262
let timeSpans (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
42174263
let moduleName = timeSpanModule com
4264+
warnOnSubMillisecondPrecisionLoss com ctx r "TimeSpan" i args
42184265

42194266
let timeSpanLibCall meth args =
42204267
let args = ignoreFormatProvider com ctx r meth args
@@ -4302,6 +4349,7 @@ let timeSpans (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (thisArg
43024349

43034350
let timeOnly (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
43044351
let moduleName = timeOnlyModule com
4352+
warnOnSubMillisecondPrecisionLoss com ctx r "TimeOnly" i args
43054353

43064354
match i.CompiledName with
43074355
| ".ctor" ->

‎tests/Integration/Compiler/IgnoredArgumentTests.fs‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,145 @@ TimeSpan.FromMilliseconds(1L, 0L) |> ignore
185185
|> Assert.Code.noWarning "FABLE0105"
186186
|> ignore
187187

188+
testCase "DateTime from non-constant ticks reports the dropped precision" <| fun _ ->
189+
let source =
190+
"""
191+
open System
192+
let f (ticks: int64) = DateTime(ticks)
193+
"""
194+
compile source
195+
|> Assert.Code.warning "FABLE0122"
196+
|> ignore
197+
198+
testCase "DateTime from constant ticks with a sub-millisecond part reports the dropped precision" <| fun _ ->
199+
let source =
200+
"""
201+
open System
202+
DateTime(630822816000012345L, DateTimeKind.Utc) |> ignore
203+
"""
204+
compile source
205+
|> Assert.Code.warning "FABLE0122"
206+
|> ignore
207+
208+
testCase "DateTime with non-zero microseconds reports the dropped precision" <| fun _ ->
209+
let source =
210+
"""
211+
open System
212+
DateTime(2020, 1, 1, 0, 0, 0, 0, 250) |> ignore
213+
DateTime(2020, 1, 1, 0, 0, 0, 0, 250, DateTimeKind.Utc) |> ignore
214+
"""
215+
compile source
216+
|> Assert.Code.warning "FABLE0122"
217+
|> ignore
218+
219+
testCase "DateTime.AddTicks with non-constant ticks reports the dropped precision" <| fun _ ->
220+
let source =
221+
"""
222+
open System
223+
let f (d: DateTime) (ticks: int64) = d.AddTicks ticks
224+
"""
225+
compile source
226+
|> Assert.Code.warning "FABLE0122"
227+
|> ignore
228+
229+
testCase "DateTimeOffset from non-constant ticks reports the dropped precision" <| fun _ ->
230+
let source =
231+
"""
232+
open System
233+
let f (ticks: int64) = DateTimeOffset(ticks, TimeSpan.Zero)
234+
"""
235+
compile source
236+
|> Assert.Code.warning "FABLE0122"
237+
|> ignore
238+
239+
testCase "DateTimeOffset with non-constant microseconds reports the dropped precision" <| fun _ ->
240+
let source =
241+
"""
242+
open System
243+
let f (us: int) = DateTimeOffset(2020, 1, 1, 0, 0, 0, 0, us, TimeSpan.Zero)
244+
"""
245+
compile source
246+
|> Assert.Code.warning "FABLE0122"
247+
|> ignore
248+
249+
testCase "DateTimeOffset.AddTicks with a sub-millisecond part reports the dropped precision" <| fun _ ->
250+
let source =
251+
"""
252+
open System
253+
let f (d: DateTimeOffset) = d.AddTicks 5L
254+
"""
255+
compile source
256+
|> Assert.Code.warning "FABLE0122"
257+
|> ignore
258+
259+
testCase "TimeSpan from non-constant ticks reports the dropped precision" <| fun _ ->
260+
let source =
261+
"""
262+
open System
263+
let f (ticks: int64) = TimeSpan ticks, TimeSpan.FromTicks ticks
264+
"""
265+
compile source
266+
|> Assert.Code.warning "FABLE0122"
267+
|> ignore
268+
269+
testCase "TimeSpan with non-zero microseconds reports the dropped precision" <| fun _ ->
270+
let source =
271+
"""
272+
open System
273+
TimeSpan(0, 0, 0, 0, 1, 250) |> ignore
274+
"""
275+
compile source
276+
|> Assert.Code.warning "FABLE0122"
277+
|> ignore
278+
279+
testCase "TimeOnly from non-constant ticks reports the dropped precision" <| fun _ ->
280+
let source =
281+
"""
282+
open System
283+
let f (ticks: int64) = TimeOnly ticks
284+
"""
285+
compile source
286+
|> Assert.Code.warning "FABLE0122"
287+
|> ignore
288+
289+
testCase "TimeOnly with non-zero microseconds reports the dropped precision" <| fun _ ->
290+
let source =
291+
"""
292+
open System
293+
TimeOnly(1, 2, 3, 4, 250) |> ignore
294+
"""
295+
compile source
296+
|> Assert.Code.warning "FABLE0122"
297+
|> ignore
298+
299+
testCase "Date and time values with no sub-millisecond part are silent" <| fun _ ->
300+
let source =
301+
"""
302+
open System
303+
DateTime(630822816000010000L) |> ignore
304+
DateTime(630822816000010000L, DateTimeKind.Utc) |> ignore
305+
DateTime(2020, 1, 1, 0, 0, 0, 0, 0) |> ignore
306+
DateTime(2020, 1, 1, 0, 0, 0, 0, 0, DateTimeKind.Utc) |> ignore
307+
DateTime(2020, 1, 1, 0, 0, 0, 5, DateTimeKind.Utc) |> ignore
308+
DateTime(2020, 1, 1).AddTicks(10000L) |> ignore
309+
DateTimeOffset(630822816000010000L, TimeSpan.Zero) |> ignore
310+
DateTimeOffset(2020, 1, 1, 0, 0, 0, 0, 0, TimeSpan.Zero) |> ignore
311+
DateTimeOffset(2020, 1, 1, 0, 0, 0, 5, TimeSpan.Zero) |> ignore
312+
DateTimeOffset(2020, 1, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(-20000L) |> ignore
313+
TimeSpan(10000L) |> ignore
314+
TimeSpan.FromTicks(0L) |> ignore
315+
TimeSpan(0, 0, 0, 0, 1, 0) |> ignore
316+
TimeSpan(1, 2, 3) |> ignore
317+
TimeOnly(10000L) |> ignore
318+
TimeOnly(1, 2, 3, 4, 0) |> ignore
319+
TimeOnly(1, 2, 3, 4) |> ignore
320+
let f (ms: int) = DateTime(2020, 1, 1, 0, 0, 0, ms), TimeSpan(0, 0, 0, 0, ms), TimeOnly(1, 2, 3, ms)
321+
let g (d: DateTime) (o: DateTimeOffset) = DateTime(d.Ticks, DateTimeKind.Utc), DateTimeOffset(o.Ticks, o.Offset)
322+
"""
323+
compile source
324+
|> Assert.Code.noWarning "FABLE0122"
325+
|> ignore
326+
188327
testCase "A private-representation flag on FSharpType is reported" <| fun _ ->
189328
let source =
190329
"""

0 commit comments

Comments
 (0)