Skip to content

Remove leftovers of the Compiler consolidation - #1861

Draft
neilvcarvalho wants to merge 3 commits into
mainfrom
nc/remove-compiler-leftovers-636d15
Draft

neilvcarvalho wants to merge 3 commits into
mainfrom
nc/remove-compiler-leftovers-636d15

Conversation

@neilvcarvalho

Copy link
Copy Markdown
Member

The Compiler consolidation (#1853) left a few classes behind that no longer do what they were built for. This pull request removes or shrinks them without changing public behavior. Each commit leaves the suite green.

Deleted

  • NullFactory (lib/factory_bot/null_factory.rb and its spec). Factory#parent now answers nil for a factory without a parent. Compiler asks Internal.compiled_configuration in that case, Factory#class_name uses safe navigation and the Linter fallback stops at nil. Compiler already treated a nil factory as the end of the parent chain for traits and enum settings. The NullFactory examples moved to factory_spec.rb and internal_spec.rb.
  • Declaration::Implicit#factory and its constructor argument. Compiler receives the definition directly, so nothing read it. The one example that compared implicit declarations by factory went with it, and the with_factory chain of the declaration matcher.

Kept, and why

  • Declaration, Declaration::Dynamic, Declaration::Association, Declaration::Implicit. After the change above they carry no behavior (no to_attributes, no build), only readers and ==. They stay as classes instead of Structs for two reasons. A bare word in a definition block returns the declaration to user code, so a Struct would add Enumerable, [], size and to_a to that value. A Struct also changes to_s, and the guards in Evaluator#increment_sequence and Syntax::Methods#increment_sequence match on the "#<FactoryBot::Declaration" prefix. Those guards detect a sequence block that interpolates an attribute name when no scope was given (sequence(:info) { |n| "#{name}" } then generate(:user, :info)), and turn it into the "Perhaps it needs a scope to operate?" error. Compiler also uses is_a?(Declaration) to report a bare word passed as factory: or as an association override.
  • AttributeList, unchanged. It is the attributes value of the factory_bot.compile_factory payload, and it is already trimmed to what subscribers can observe: enumeration, names, associations, transient and non_transient. A plain Array cannot keep those readers.
  • Strategy::Null, unchanged and still registered as :null. It is not only a public strategy: Strategy::AttributesFor#association runs associations with runner.run(:null), so attributes_for depends on it.
  • spec/support/matchers/declaration.rb and its include in spec_helper.rb, because definition_proxy_spec.rb still asserts on the declarations the proxy records.

Verification

  • bundle exec rake all_specs on the default gemfile (Ruby 4.0.10): 312 unit examples, 457 acceptance examples, 4 scenarios, 0 failures.
  • BUNDLE_GEMFILE=gemfiles/7.0.gemfile bundle exec rake all_specs: same counts, 0 failures.
  • BUNDLE_GEMFILE=gemfiles/main.gemfile bundle exec rake all_specs: same counts, 0 failures.
  • bundle exec standardrb: 147 files inspected, no offenses.

No file under spec/acceptance was edited.

Declaration::Implicit kept a reference to the definition it was
declared in. That was needed when the declaration resolved itself into
an attribute. Compiler now does that work and receives the definition
directly, so nothing reads the reference any more.

This commit removes the reader and the constructor argument, which
leaves the three declaration classes as plain records of what a factory
or trait block declared. They stay as classes instead of becoming
Structs because a bare word in a definition block returns the
declaration to user code. A Struct would add Enumerable and index
access to that value and change its to_s, which the sequence guards in
Evaluator and Syntax::Methods match on to report a missing scope.

There are no side effects for users. The spec example that compared
implicit declarations by factory is removed with the attribute.
Factory#parent answered a NullFactory when a factory had no parent.
After the Compiler consolidation that object only supplied a nil class
name and the compiled global configuration, plus an empty definition
that the trait and enum lookups walked for no result.

Factory#parent now answers nil without a parent. Compiler asks
Internal for the compiled configuration in that case, the class name
lookup uses safe navigation, and the Linter fallback stops at nil.
Compiler already treated a nil factory as the end of the parent chain
when collecting traits and enum settings.

Factory is private API, so there are no side effects for users. The
NullFactory examples move to the Factory and Internal specs, where the
behavior now lives.
Compiler#inherited shared its name with a local variable in
traits_in_scope_of that holds the parent's traits. The local shadowed
the method, so one name meant a CompiledFactory in one place and an
Array of traits in another. The method is now parent_compiled.

The compiled_configuration examples compared the snapshot against
untouched global defaults, so they would pass even if the snapshot
ignored the configuration. They now register a global callback,
constructor and to_create before reading the snapshot.

There are no side effects on behavior.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant