forked from harttle/liquidjs
-
Notifications
You must be signed in to change notification settings - Fork 5
fix(SPD-23149): nil-safe bracket access in scope.js + nil-safe filters for sd_order_details sd_unit crash #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sd-gh-bot
wants to merge
1
commit into
master
Choose a base branch
from
fix/sd-order-details-nil-safe-scope-filters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * Regression tests for SPD-23149: | ||
| * computeColumn sd_unit with bracket accessor on nil self.sd_product | ||
| * should not crash — mirrors the Wingify/VWO Order Details / sd_unit failure. | ||
| * | ||
| * Before fix: | ||
| * unit_mapping[self.sd_product] where self.sd_product is nil/undefined | ||
| * caused: TypeError: Cannot read properties of undefined (reading 'length') | ||
| * at push() in scope.js propertyAccessSeq — blocked all document generation. | ||
| * | ||
| * After fix: | ||
| * nil bracket key is coerced to '' → path collapses to ['unit_mapping'] → | ||
| * returns the mapping object instead of crashing. downcase/size/first/last/join | ||
| * filters also guard nil inputs. | ||
| */ | ||
| const chai = require('chai') | ||
| const expect = chai.expect | ||
| const Liquid = require('../../index.js') | ||
|
|
||
| describe('computeColumn nil-safe bracket access (SPD-23149)', function () { | ||
| let engine | ||
|
|
||
| beforeEach(function () { | ||
| engine = Liquid({ root: __dirname }) | ||
| }) | ||
|
|
||
| it('should compute sd_unit via unit_mapping lookup correctly when sd_product is set', async function () { | ||
| const tpl = ` | ||
| {% computeColumn sd_order_details sd_unit %} | ||
| {% parseAssign unit_mapping = '{"VWO Rollouts - Web":"sessions","VWO Testing - Web":"users"}' %} | ||
| {% assign $$answer = unit_mapping[self.sd_product] %} | ||
| {% endcomputeColumn %} | ||
| ` | ||
| const ctx = { | ||
| sd_order_details: [ | ||
| { sd_product: 'VWO Rollouts - Web', sd_quota: 100 }, | ||
| { sd_product: 'VWO Testing - Web', sd_quota: 50 }, | ||
| ] | ||
| } | ||
| await engine.parseAndRender(tpl, ctx) | ||
| expect(ctx.sd_order_details[0].sd_unit).to.equal('sessions') | ||
| expect(ctx.sd_order_details[1].sd_unit).to.equal('users') | ||
| }) | ||
|
|
||
| it('should NOT crash when sd_product is missing from a row (nil bracket-variable safety)', async function () { | ||
| const tpl = ` | ||
| {% computeColumn sd_order_details sd_unit %} | ||
| {% parseAssign unit_mapping = '{"VWO Rollouts - Web":"sessions"}' %} | ||
| {% assign $$answer = unit_mapping[self.sd_product] %} | ||
| {% endcomputeColumn %} | ||
| ` | ||
| const ctx = { | ||
| sd_order_details: [ | ||
| { sd_quota: 100 } // sd_product intentionally missing | ||
| ] | ||
| } | ||
| // Before the fix: TypeError: Cannot read properties of undefined (reading 'length') | ||
| // After the fix: no throw; formula completes (value may be the fallback mapping object) | ||
| let threw = false | ||
| try { | ||
| await engine.parseAndRender(tpl, ctx) | ||
| } catch (e) { | ||
| threw = true | ||
| } | ||
| expect(threw).to.equal(false) | ||
| }) | ||
|
|
||
| it('should NOT crash when downcase is applied to a nil sd_product field', async function () { | ||
| const tpl = ` | ||
| {% computeColumn sd_order_details sd_unit %} | ||
| {% assign lc = self.sd_product | downcase %} | ||
| {% if lc == 'vwo rollouts - web' %} | ||
| {% assign $$answer = 'sessions' %} | ||
| {% else %} | ||
| {% assign $$answer = 'unknown' %} | ||
| {% endif %} | ||
| {% endcomputeColumn %} | ||
| ` | ||
| const ctx = { | ||
| sd_order_details: [ | ||
| { sd_product: 'VWO Rollouts - Web' }, | ||
| {} // no sd_product — previously crashed at | downcase on nil | ||
| ] | ||
| } | ||
| await engine.parseAndRender(tpl, ctx) | ||
| expect(ctx.sd_order_details[0].sd_unit).to.equal('sessions') | ||
| expect(ctx.sd_order_details[1].sd_unit).to.equal('unknown') | ||
| }) | ||
|
|
||
| it('should process all rows even when one row has missing fields', async function () { | ||
| const tpl = ` | ||
| {% computeColumn sd_order_details sd_unit %} | ||
| {% parseAssign unit_mapping = '{"VWO Rollouts - Web":"sessions","VWO Testing - Web":"users"}' %} | ||
| {% assign $$answer = unit_mapping[self.sd_product] %} | ||
| {% endcomputeColumn %} | ||
| ` | ||
| const ctx = { | ||
| sd_order_details: [ | ||
| { sd_product: 'VWO Rollouts - Web', sd_quota: 500 }, | ||
| { sd_quota: 100 }, // missing sd_product — should not crash entire table | ||
| { sd_product: 'VWO Testing - Web', sd_quota: 200 }, | ||
| ] | ||
| } | ||
| let threw = false | ||
| try { | ||
| await engine.parseAndRender(tpl, ctx) | ||
| } catch (e) { | ||
| threw = true | ||
| } | ||
| expect(threw).to.equal(false) | ||
| expect(ctx.sd_order_details[0].sd_unit).to.equal('sessions') | ||
| // row[1] sd_unit will be the fallback value (whole mapping), not crash | ||
| expect(ctx.sd_order_details[2].sd_unit).to.equal('users') | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These nil-safety guards repeat the same
v == null ? <fallback> : <expression>pattern acrossdowncase,first,join,last, andsize, should we extract a sharednilSafehelper likeconst nilSafe = (fn, fallback) => v => (v == null ? fallback : fn(v))?Want Baz to fix this for you? Activate Fixer