posthtml / posthtml-expressions

Use variables, JS-like expressions, and even markup-powered logic in your HTML.
Other
123 stars 20 forks source link

Added `missingLocal` option to define how to handle missing local value #138

Closed b-gyula closed 1 year ago

b-gyula commented 1 year ago

Proposed Changes

Previously in case a value for a referenced local was missing the string 'undefined' was inserted in the output EXCEPT if it was used in an expression like {{missingLocal ? 'yes' : 'no'}} when an exception is thrown.

  1. This is inconsistent behavior
  2. Silently generating string 'undefined' makes difficult to detect undesired behavior. A warning log or reference in the output makes it much more easier to detect.

I was tying to create a opportunity for developers to decide how to handle such situation. The default behavior is changed: when strictMode is true (default) and leaving the new missingLocal option undefined, then the same "'foo' is not defined" exception is thrown as when a referenced value is missing in an expression

Setting strictMode false and leaving the new missingLocal option undefined results the previous result: undefined

Setting the new missingLocal option to a string will produce that string in the output regardless the value of strictMode. missingLocal can contain the placeholder {local} which will be replaced with the name of the missing local in the output. This solution allows to

  1. Completely ignore missing locals by setting missingLocal to ""
  2. Include the name of the missing local in the output to help detect the which value is missing in locals like "#Missing value: {local}"

See updated README and test cases

Types of Changes

What types of changes does your code introduce

Checklist

Further Comments

If this is a large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

Reviewers: @mrmlnc, @jescalan, @michael-ciniawsky, @posthtml/collaborators