Dependency order arranges attributes so that no attribute appears before another attribute it depends on. This ordering lets generated initialization assignments execute safely. section 9.3.4
Apply the ordering rule as follows:
- Reverse each dependency chain: for 1 → 2, initialize 2 before 1. section 9.3.4
- Visit dependencies before adding the current attribute to the ordered list. section 9.3.4
- Use topological sorting to process every attribute, including attributes in incomplete or independent chains. section 9.3.4
Correcting generated initialization
If attribute1 refers to attribute2, generating attribute1 first is out of order. The corrected Rental initialization is: attribute2 = 42, then attribute1 = this.attribute2. section 9.3.4
Listing 9.5 The initial version of the dependencyOrderOf function
const { referencedAttributesInValueOf } = require("../language/queries")
const dependencyOrderOf = (attributes) => {
const ordered = [] #1
const visit = (current, chain) => { #2
if (ordered.indexOf(current) > -1) { #3
return
}
const extendedChain = [ ...chain, current ] #4
referencedAttributesInValueOf(current).forEach(
(dependency) =>
visit(dependency, extendedChain) #5
)
ordered.push(current) #6
}
attributes.forEach(
(attribute) => visit(attribute, []) #7
)
return ordered
}
module.exports.dependencyOrderOf = dependencyOrderOf
The recursive implementation tracks visited attributes, follows referenced attributes first, and appends each attribute afterward. section 9.3.4
