HandleFaultyContent strengthens a DSL generator so incomplete or invalid AST content does not make it crash. Instead of assuming every value has the expected shape, generator helpers check their inputs and emit a fallback value or problem marker, allowing generation to continue. section 9.2.1 section 9.4
Use it alongside constraints, not as a substitute for them. Constraints tell domain experts what to fix; fault handling protects generation when reported issues are still present. The resulting code may be invalid or meaningless, so completing generation is only an intermediate success. section 9.4 section 9.2.1
Implement the strategy defensively:
- Identify generator functions that assume a string, AST object, or reference is present. section 9.2.2 section 9.3.2
- Guard each assumption before calling methods such as charAt or reading nested settings. section 9.2.2 section 9.3.2
- Return a conspicuous fallback identifier or generated-code comment that records the generation problem. section 9.4 section 9.3.6
- Add constraints that report the underlying missing or invalid property to the domain expert. section 9.4 section 9.3.2
Missing attribute name
A missing name can reach camelCase as undefined. Rather than throwing, camelCase returns a diagnostic-looking identifier. withFirstUpper needs the same guard; otherwise it can still fail on undefined. Generation then finishes, while the missing name remains an issue to resolve. section 9.2.2 section 9.2.1
Listing 9.12 The changed content in src/generator/template-utils.js
const camelCase = (str) => typeof str === "string" #1
? str
.toLowerCase()
.replace(/\s+([a-z])/g, (_, ch) => ch.toUpperCase())
.replace(" ", "")
: "__generationProblemDueToNonStringArgumentToCamelCase"
module.exports.camelCase = camelCase
const withFirstUpper = (str) => typeof str === "string"
? str.charAt(0).toUpperCase() + str.substring(1)
: "GenerationProblemDueToNonStringArgumentToWithFirstUpper"
module.exports.withFirstUpper = withFirstUpper
Compare guarded camelCase and withFirstUpper implementations: each preserves normal string handling while returning a named fallback for non-string input. section 9.3.6
