<commit_before>package terraform

import (
	"fmt"

	"github.com/hashicorp/terraform/config"
	"github.com/hashicorp/terraform/dag"
)

// OrphanTransformer is a GraphTransformer that adds orphans to the
// graph. This transformer adds both resource and module orphans.
type OrphanTransformer struct {
	// State is the global state. We require the global state to
	// properly find module orphans at our path.
	State *State

	// Config is just the configuration of our current module.
	Config *config.Config
}

func (t *OrphanTransformer) Transform(g *Graph) error {
	state := t.State.ModuleByPath(g.Path)
	if state == nil {
		// If there is no state for our module, there can't be any orphans
		return nil
	}

	// Go over each resource orphan and add it to the graph.
	for _, k := range state.Orphans(t.Config) {
		g.ConnectTo(
			g.Add(&graphNodeOrphanResource{ResourceName: k}),
			state.Resources[k].Dependencies)
	}

	// Go over each module orphan and add it to the graph. We store the
	// vertexes and states outside so that we can connect dependencies later.
	moduleOrphans := t.State.ModuleOrphans(g.Path, t.Config)
	moduleVertexes := make([]dag.Vertex, len(moduleOrphans))
	moduleStates := make([]*ModuleState, len(moduleVertexes))
	for i, path := range moduleOrphans {
		moduleVertexes[i] = g.Add(&graphNodeOrphanModule{Path: path})
		moduleStates[i] = t.State.ModuleByPath(path)
	}

	// Module dependencies
	for i, v := range moduleVertexes {
		g.ConnectTo(v, moduleStates[i].Dependencies)
	}

	return nil
}

// graphNodeOrphanModule is the graph vertex representing an orphan resource..
type graphNodeOrphanModule struct {
	Path []string
}

func (n *graphNodeOrphanModule) Name() string {
	return fmt.Sprintf("module.%s (orphan)", n.Path[len(n.Path)-1])
}

// graphNodeOrphanResource is the graph vertex representing an orphan resource..
type graphNodeOrphanResource struct {
	ResourceName string
}
<commit_msg>terraform: move resource orphan deps out of loop<commit_after>package terraform

import (
	"fmt"

	"github.com/hashicorp/terraform/config"
	"github.com/hashicorp/terraform/dag"
)

// OrphanTransformer is a GraphTransformer that adds orphans to the
// graph. This transformer adds both resource and module orphans.
type OrphanTransformer struct {
	// State is the global state. We require the global state to
	// properly find module orphans at our path.
	State *State

	// Config is just the configuration of our current module.
	Config *config.Config
}

func (t *OrphanTransformer) Transform(g *Graph) error {
	state := t.State.ModuleByPath(g.Path)
	if state == nil {
		// If there is no state for our module, there can't be any orphans
		return nil
	}

	// Go over each resource orphan and add it to the graph.
	resourceOrphans := state.Orphans(t.Config)
	resourceVertexes := make([]dag.Vertex, len(resourceOrphans))
	for i, k := range resourceOrphans {
		resourceVertexes[i] = g.Add(&graphNodeOrphanResource{ResourceName: k})
	}

	// Go over each module orphan and add it to the graph. We store the
	// vertexes and states outside so that we can connect dependencies later.
	moduleOrphans := t.State.ModuleOrphans(g.Path, t.Config)
	moduleVertexes := make([]dag.Vertex, len(moduleOrphans))
	moduleStates := make([]*ModuleState, len(moduleVertexes))
	for i, path := range moduleOrphans {
		moduleVertexes[i] = g.Add(&graphNodeOrphanModule{Path: path})
		moduleStates[i] = t.State.ModuleByPath(path)
	}

	// Now do the dependencies. We do this _after_ adding all the orphan
	// nodes above because there are cases in which the orphans themselves
	// depend on other orphans.

	// Resource dependencies
	for i, v := range resourceVertexes {
		g.ConnectTo(v, state.Resources[resourceOrphans[i]].Dependencies)
	}

	// Module dependencies
	for i, v := range moduleVertexes {
		g.ConnectTo(v, moduleStates[i].Dependencies)
	}

	return nil
}

// graphNodeOrphanModule is the graph vertex representing an orphan resource..
type graphNodeOrphanModule struct {
	Path []string
}

func (n *graphNodeOrphanModule) Name() string {
	return fmt.Sprintf("module.%s (orphan)", n.Path[len(n.Path)-1])
}

// graphNodeOrphanResource is the graph vertex representing an orphan resource..
type graphNodeOrphanResource struct {
	ResourceName string
}

{'fraction_non_alphanumeric': 0.10096493292539421, 'fraction_numerical': 0.0004706989879971758, 'mean_word_length': 4.279503105590062, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5424062', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1313, 'n_words': 438}