Skip to content

Commit

Permalink
Support validation checks for Constants with units of measure types
Browse files Browse the repository at this point in the history
  • Loading branch information
dawedawe committed Aug 22, 2023
1 parent 18b5e8c commit 35a4825
Show file tree
Hide file tree
Showing 62 changed files with 407 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,49 @@ type UpdateLiteralConstantInSignatureFix(error: LiteralConstantValuesDifferInSig
let mutable sigRefPat = null

let rec isImplExprValidInSig (implExpression: IFSharpExpression) =

let opName = $"{nameof UpdateLiteralConstantInSignatureFix}.IsAvailable"

let rec collectUofMRefs (uOfM: IUnitOfMeasure) =
seq {
match uOfM with
| :? INamedMeasure as named ->
yield named.TypeUsage.As<INamedTypeUsage>().ReferenceName.Reference
| :? IProductMeasure as product ->
yield! collectUofMRefs product.Measure1
yield! collectUofMRefs product.Measure2
| :? ISeqMeasure as seqM ->
for m in seqM.Measures do
yield! collectUofMRefs m
| :? IDivideMeasure as divide ->
yield! collectUofMRefs divide.Measure1
yield! collectUofMRefs divide.Measure2
| :? IPowerMeasure as power ->
yield! collectUofMRefs power.Measure
| :? IParenMeasure as paren ->
yield! collectUofMRefs paren.Measure
| _ -> ()
}

let isValidUofMInSig (uOfM: IUnitOfMeasureClause) =
if isNull uOfM.Measure then false else

let refs = collectUofMRefs uOfM.Measure
refs |> Seq.forall (
fun r -> r.ResolveWithFcs(
sigRefPat, opName, false, true)
|> Option.isSome)


match implExpression.IgnoreInnerParens() with
| :? IReferenceExpr as refExpr ->
refExpr.Reference.ResolveWithFcs(
sigRefPat, $"{nameof UpdateLiteralConstantInSignatureFix}.IsAvailable", true, true)
sigRefPat, opName, true, true)
|> Option.isSome
| :? IBinaryAppExpr as binExpr ->
isImplExprValidInSig binExpr.LeftArgument && isImplExprValidInSig binExpr.RightArgument
| :? ILiteralExpr as litExpr -> isNull litExpr.UnitOfMeasure
| :? ILiteralExpr as litExpr ->
if isNull litExpr.UnitOfMeasure then true else isValidUofMInSig litExpr.UnitOfMeasure
| _ -> false

override x.Text =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<m> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m * kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m * kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<m> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<m> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42< / m>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42< / m>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23< / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<m> = 23< / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m^2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m^2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<(m * kg)>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<(m * kg)>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<kg> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type kg

[<Literal>]
val a : int<m> = 23<kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module A

[<Measure>] type m
[<Measure>] type kg
[<Measure>] type s

[<Literal>]
let a{caret} = 42<m kg s>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module A

[<Measure>] type m
[<Measure>] type kg
[<Measure>] type s

[<Literal>]
let a{caret} = 42<m kg s>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
val a : int<m kg> = 23<m kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
val a : int<m kg> = 23<m kg>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ module A
[<Measure>] type m

[<Literal>]
val a : int<m> = 23<m>
val a : int<m> = 42<m>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type s

[<Literal>]
let a{caret} = 42<m * s>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type s

[<Literal>]
let a{caret} = 42<m * s>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type s

[<Literal>]
val a : int<m * s> = 23<m * s>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type s

[<Literal>]
val a : int<m * s> = 42<m * s>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
let a{caret} = 42<m / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
val a : int<m / kg> = 23<m / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module A

[<Measure>] type m
[<Measure>] type kg

[<Literal>]
val a : int<m / kg> = 42<m / kg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type m

[<Literal>]
let a{caret} = 42<m^2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type m

[<Literal>]
let a{caret} = 42<m^2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type m

[<Literal>]
val a : int<m^2> = 23<m^2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module A

[<Measure>] type m

[<Literal>]
val a : int<m^2> = 42<m^2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module A

[<Literal>]
let a{caret} = 42<1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module A

[<Literal>]
let a{caret} = 42<1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module A

[<Literal>]
val a : int<1> = 23<1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module A

[<Literal>]
val a : int<1> = 42<1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module A

[<Literal>]
let a{caret} = 42<_>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module A

[<Literal>]
let a{caret} = 42<_>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module A

[<Literal>]
val a : int<_> = 23<_>
Loading

0 comments on commit 35a4825

Please sign in to comment.