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

add go/types based template type checking #17

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7e32522
move script stuff to separate file
crhntr Nov 30, 2024
24e21f8
add template tree walking to find malformed method calls
crhntr Nov 28, 2024
af9f7e7
feat: type check number and bool arguments
crhntr Dec 1, 2024
36460d3
check untyped float parameters
crhntr Dec 1, 2024
32673a5
add integer types
crhntr Dec 1, 2024
36c2500
refactor: remove NoErr ErrorCheck functions
crhntr Dec 1, 2024
b90133f
start adding muxt template type checking
crhntr Dec 1, 2024
350a6cf
add node types for example
crhntr Dec 1, 2024
4af91e6
refactor: use interface for tree lookup instead of map
crhntr Dec 1, 2024
280d970
factor out check functions into separate functions
crhntr Dec 1, 2024
9768047
use methods on scope instead of functions
crhntr Dec 1, 2024
809451b
type check key value range variables
crhntr Dec 2, 2024
44b4ac9
refactor: rename helper type to Forrest
crhntr Dec 2, 2024
f1a4ff5
refactor: add helper to load package
crhntr Dec 2, 2024
e151709
fix find package
crhntr Dec 2, 2024
e9ddd5c
refactor: remove always ni types.Type result
crhntr Dec 2, 2024
8985cc1
integrate check into generate
crhntr Dec 2, 2024
a04ced5
fix: nil pointer dereference when packages.NeedTypes is not set
crhntr Dec 2, 2024
4151a27
feat: add runtime assertion package
crhntr Dec 21, 2024
6e4cb91
feat: typecheck function calls
crhntr Dec 21, 2024
9f11bc5
feat: improve if/with else node type check
crhntr Dec 21, 2024
ff6bdbd
feat(typecheck): support chain nodes
crhntr Dec 21, 2024
d79287c
move types to a separate file
crhntr Dec 21, 2024
f938eca
fix: check nil command error
crhntr Dec 21, 2024
47170ef
feat(check template): add more tests
crhntr Dec 21, 2024
383e40a
refactor(check template): rename T so we can use it for copied data
crhntr Dec 21, 2024
7f1ef76
add more tests from stdlib
crhntr Dec 21, 2024
fa89941
refactor: rename to match stdlib
crhntr Dec 21, 2024
021d09b
add all stdlib tests without rewrite
crhntr Dec 24, 2024
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
8 changes: 6 additions & 2 deletions cmd/muxt/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import (
"github.com/crhntr/muxt/internal/configuration"
)

const CodeGenerationComment = "// Code generated by muxt. DO NOT EDIT."
const (
CodeGenerationComment = "// Code generated by muxt. DO NOT EDIT."
experimentCheckTypesEnvVar = "MUXT_EXPERIMENT_CHECK_TYPES"
)

func generateCommand(args []string, workingDirectory string, stdout, stderr io.Writer) error {
func generateCommand(workingDirectory string, args []string, getEnv func(string) string, stdout, stderr io.Writer) error {
config, err := configuration.NewRoutesFileConfiguration(args, stderr)
if err != nil {
return err
}
config.ExperimentalCheckTypes = getEnv(experimentCheckTypesEnvVar) == "true"
s, err := muxt.TemplateRoutesFile(workingDirectory, log.New(stdout, "", 0), config)
if err != nil {
return err
Expand Down
26 changes: 2 additions & 24 deletions cmd/muxt/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package main

import (
"bytes"
"flag"
"fmt"
"io"
"os"

"rsc.io/script"
)

func main() {
Expand All @@ -19,11 +16,11 @@ func main() {
os.Exit(handleError(command(wd, flag.Args(), os.Getenv, os.Stdout, os.Stderr)))
}

func command(wd string, args []string, _ func(string) string, stdout, stderr io.Writer) error {
func command(wd string, args []string, getEnv func(string) string, stdout, stderr io.Writer) error {
if len(args) > 0 {
switch cmd, cmdArgs := args[0], args[1:]; cmd {
case "generate", "gen", "g":
return generateCommand(cmdArgs, wd, stdout, stderr)
return generateCommand(wd, cmdArgs, getEnv, stdout, stderr)
case "version", "v":
return versionCommand(stdout)
}
Expand All @@ -38,22 +35,3 @@ func handleError(err error) int {
}
return 0
}

func scriptCommand() script.Cmd {
return script.Command(script.CmdUsage{
Summary: "muxt",
Args: "",
}, func(state *script.State, args ...string) (script.WaitFunc, error) {
return func(state *script.State) (string, string, error) {
var stdout, stderr bytes.Buffer
err := command(state.Getwd(), args, func(s string) string {
e, _ := state.LookupEnv(s)
return e
}, &stdout, &stderr)
if err != nil {
stderr.WriteString(err.Error())
}
return stdout.String(), stderr.String(), err
}, nil
})
}
29 changes: 29 additions & 0 deletions cmd/muxt/script.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"bytes"

"rsc.io/script"
)

func scriptCommand() script.Cmd {
return script.Command(script.CmdUsage{
Summary: "muxt",
Args: "",
}, func(state *script.State, args ...string) (script.WaitFunc, error) {
return func(state *script.State) (string, string, error) {
var stdout, stderr bytes.Buffer
err := command(state.Getwd(), args, func(s string) string {
if s == experimentCheckTypesEnvVar {
return "true"
}
e, _ := state.LookupEnv(s)
return e
}, &stdout, &stderr)
if err != nil {
stderr.WriteString(err.Error())
}
return stdout.String(), stderr.String(), err
}, nil
})
}
25 changes: 2 additions & 23 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/crhntr/dom v0.1.0-dev.5 h1:/7joIQhGxSKbuJyD1xDfheT0/wBdBSTXuRh+mLMqqxM=
github.com/crhntr/dom v0.1.0-dev.5/go.mod h1:4czJjiQOqiFq99bP/dfi6hjFXKnFptRyqwC8PJNQQkY=
github.com/crhntr/dom v0.1.0-dev.6 h1:iUkl5c1i3QRRyYjdozGDuNnYdEQzZp1sFk9QTmFrO4c=
github.com/crhntr/dom v0.1.0-dev.6/go.mod h1:V2RcN/d7pdUo5romb+mk/K4nm4QwAmwuJ259vdJGE/M=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand All @@ -21,8 +18,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand All @@ -37,23 +32,16 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -62,18 +50,14 @@ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand All @@ -83,7 +67,6 @@ golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXct
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
Expand All @@ -104,10 +87,6 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
27 changes: 27 additions & 0 deletions internal/assert/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package assert

import (
"fmt"
"os"
)

const pleaseCreateAnIssue = `If you get this error, please file an issue on https://github.com/crhntr/muxt/issues/new with a description of the inputs. I did not expect this to be possible.`

func Exit(description, message string, in ...any) {
_, _ = fmt.Fprintf(os.Stderr, message, in...)
_, _ = fmt.Fprintf(os.Stderr, "\n"+description)
_, _ = fmt.Fprintln(os.Stderr, "\n"+pleaseCreateAnIssue)
os.Exit(1)
}

func Len[T any](in []T, n int, description string) {
if len(in) != n {
Exit(description, "expected length %d got %d\n", n, len(in))
}
}

func MaxLen[T any](in []T, n int, description string) {
if len(in) > n {
Exit(description, "expected length less than %d got %d\n", n, len(in))
}
}
Loading
Loading