fix: scope duplicate key tracking per section with bracketedArray=false#299
fix: scope duplicate key tracking per section with bracketedArray=false#299
Conversation
Fixes npm#298 When using `bracketedArray: false`, the duplicate key counter was global across all sections, causing keys with the same name in different sections to be incorrectly converted to arrays. For example: ```ini [section_1] var = 1 [section_2] var = 2 ``` Was incorrectly parsed as: ```js { section_1: { var: '1' }, section_2: { var: ['2'] } } ``` The fix resets the duplicates counter when entering a new section, so keys are only treated as duplicates when they appear multiple times within the same section. Now correctly parses as: ```js { section_1: { var: '1' }, section_2: { var: '2' } } ```
|
Purely as an "is it allowed"... [section_1]
var = 1
[section_2]
var = 2
[section_1]
var = 3I can't remember the rules on a section being reused in the file, If so, |
|
@rquadling - Great question! I tested this exact scenario. With a repeated section: [section_1]
var = 1
[section_2]
var = 2
[section_1]
var = 3With this fix ( On So this fix makes |
Summary
Fixes #298
When using
bracketedArray: false, the duplicate key counter was global across all sections, causing keys with the same name in different sections to be incorrectly converted to arrays.The Problem
Was incorrectly parsed as:
Because the global
duplicatescounter sawvartwice across both sections.The Fix
Reset the
duplicatescounter when entering a new section (line 128), so keys are only treated as duplicates when they appear multiple times within the same section.Now correctly parses as:
Changes
lib/ini.js: Changeduplicatesfromconsttolet, reset to{}when entering new sectiontest/duplicate-properties.js: Add regression tests for cross-section and within-section duplicatesTest Results
All tests pass with 100% coverage.