Skip to content

Enhancement Request: support extracting expected program inputs #375

Answered by scosman
scosman asked this question in General
Discussion options

You must be logged in to vote

Here's what I ended up with. Had to dive into internals a bit to get this working with config/methods/optimizer, might be handy as a tested part of the main project, but I leave that up to you!

type cmAnalysisVisitor struct {
	variables []string
}

func (v *cmAnalysisVisitor) Visit(n *ast.Node) {
	if node, ok := (*n).(*ast.IdentifierNode); ok {
		if !node.Method {
			v.variables = append(v.variables, node.Value)
		}
	}
}

func extractVariablesFromCode(code string) ([]string, error) {
	tree, err := parser.Parse(code)
	if err != nil {
		return nil, err
	}

	config := conf.New(cmExprEnv{})
	config.Strict = false
	_, err = checker.Check(tree, config)
	if err != nil {
		return nil, err
	}
	err…

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by antonmedv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #374 on May 24, 2023 11:46.